uses loops and recursion to calculate factorials