I originally solved the problem with this:
def factorial(x): result = 1 if x >= 0 and x == int(x): while x > 0: result *= x x -= 1 return result else: return "You have to use a positive integer!"
But I wanted to know if there was a better way to do this (my method seemed not so smart), so I checked out the hint, which suggested letting factorial() call itself. Checked out the forum and learned it was a recursion. Anyhow I eventually ended up with this:
def factorial(x): if x >= 2 and x == int(x): return x * factorial(x-1) elif x == 1 or x == 0: return 1 else: return "You have to use a positive integer!"
However, this recursion thing seemed rather unintuitive (logically complex for me) and thus not that readable (for me)....
I am curious about which was the better way, and this really bothered me so I lost some time googling to find a way to time the two functions' performance. I didn't understand how to use
import timeit and thus kept failing to make it work, but I eventually ended up finding an easy way to do this in IPython (and I had it installed in my computer), and I got this:
""" Used x = 500 because when using x = 1000 with the recursion solution gave me a max recursion depth error... This just deepend my feeling that recursion wasn't really a great idea... """ In : x = 500 In : %timeit factorial_1(x) # the hint's suggested solution 1000 loops, best of 3: 222 µs per loop In : %timeit factorial_2(x) # my own original solution 10000 loops, best of 3: 115 µs per loop # Just for the heck of it In : import math In : %timeit math.factorial(x) 100000 loops, best of 3: 13.2 µs per loop
I don't know why the loops are different, but anyhow, it looks like my original solution was stupid but still about 2x faster than the recursion solution i.e. recursion is resource expensive and thus not a great idea for this kind of problem (I wonder when it is a good idea to use it...hopefully I'll learn more in the future = =).
Did the hint just suggest using recursion to teach us something new or is this recursion somehow a better solution than what I used initially and why?
If recursion is not a good solution, is there a better way to solve this factorial problem than what I used initially? (Based on what we've learned so far and ignoring