Recursion is very deceiving. It looks simple but it is anything but from the computer's standpoint, being very resource intensive. Yes, it is important to understand recursive functions, how they work and how to write them. Just don't be fooled into thinking them simple. There is a reason they are not discussed in these introductory tracks. In most cases, we can write an algorithm that is comparably fast, or faster, and in most cases more efficient.
Take a gander at this post I compared several factorial functions to see how fast they were.
Just for fun @jirapong_p I will add your time to this post, I had to change the max recursion depth because of your code though.
if x == 0 or x == 1:
return (x * factorial(x - 1))
This function saves the factorial to file. It also calls the function
ranging from 1 to the given number. So all factorials to the number are
saved to file. The computer has problems displaying any number
the system can not handle.
:param num_range: Range of numbers to be factored
with open("factorial_of_%s.txt" % str(num_range), "w+") as file:
for number in range(1, num_range + 1):
file.write(str(factorial(number)) + "\n")
I'm trying to visualize this....is this kind of like Inception (a function within a function within a function)? So you keep stepping into another factorial function ( in -1 increments) and once you return 1 it reiterates backwards and performs the multiplication? If anyone could explain the exact steps here, it would be much appreciated.