Oct-11-2020, 01:12 PM
(This post was last modified: Oct-11-2020, 01:12 PM by Drone4four.
Edit Reason: Correct ` formatting x3
)
The “factorial” of the integer 5 is the product of the first 5 integers multiplied against each other. So, 1*2*3*4*5 = 120.
Here is a script modelling the factorial of 5 using a for loop:
For every iteration through the loop at lines 3 and 4, the factorial variable is redefined, where it is multiplied against itself by the iterator, i. So factorial on the first iteration goes from 1 to 1, and is then modified to become 2, which becomes 6, then 24, then 120. Once the range is exhausted, the script returns the product, which is the end result. That makes sense I think.
Here is a factorial script which uses recursion:
I believe this recursive model for factorial multiplies backwards from the highest number to the least. For example, 5*4*3*2*1 = end product. Once it reaches 1, I’d expect that this function to return 1. When I follow along in my debugger, n slowly does count down from 5 to 1 (as I expect) however what baffles and perplexes me now is how or why the function returns 120 because as n descends from 5, n just decrements until it reaches 1 and then returns True (1) when the conditional verifies that n == 1. So no matter how large the initial parameter, I’d expect this script just to keep counting down to 1. So the output, regardless of the integer value passed in, should always be the same: 1 (or True). So this recursive algorithm is less of a factorial calculator and more just useless scripts which counts backward from n. Yet miraculously it actually returns the correct factorial. Could someone clarify what is going on here in this recursive script?
According to programiz, when passing in 3 as the argument (instead of 5), here is another interpretation of what is going on in the recursive script:
What I think is throwing me off is that my debugger in VSC for the original for loop implementation (above) tosses three variables around at code execution: factorial, i, and num. With each iteration, my debugger shows each variable increment, which is enormously helpful to observe how factorials work in general. But when executing the recursive script, my debugger only shows n decrement at each pass which makes it more difficult to observe the recursive operation.
As Wikipedia explains, there are many advanced topics involving factorials and number theory which is way beyond my 8th grade-level proficiency in math.
For what it is worth, here is my valiant (but failed) attempt at converting the original for loop into list comprehension:
If someone would kindly correct my list comprehension attempt in this context, that I think might help too.
Here is a script modelling the factorial of 5 using a for loop:
1 2 3 4 5 6 7 8 9 |
def fact(num): factorial = 1 for i in range ( 1 , num + 1 ): factorial = factorial * i return factorial if __name__ = = "__main__" : fact( 5 ) |
Here is a factorial script which uses recursion:
1 2 3 4 5 6 7 8 9 10 11 |
def factorial_recursive(n): # Base case: 1! = 1 if n = = 1 : return 1 # Recursive case: n! = n * (n-1)! else : return n * factorial_recursive(n - 1 ) if __name__ = = "__main__" : factorial_recursive( 5 ) |
According to programiz, when passing in 3 as the argument (instead of 5), here is another interpretation of what is going on in the recursive script:
1 2 3 4 5 6 |
factorial( 3 ) # 1st call with 3 3 * factorial( 2 ) # 2nd call with 2 3 * 2 * factorial( 1 ) # 3rd call with 1 3 * 2 * 1 # return from 3rd call as number=1 3 * 2 # return from 2nd call 6 # return from 1st call |
As Wikipedia explains, there are many advanced topics involving factorials and number theory which is way beyond my 8th grade-level proficiency in math.
For what it is worth, here is my valiant (but failed) attempt at converting the original for loop into list comprehension:
1 2 3 4 5 6 |
def fact(num): factorial = 1 return [factorial * i for i in range ( 1 , num + 1 )] if __name__ = = "__main__" : fact( 5 ) |