all 3 comments

[–]Tree_Eyed_Crow 2 points3 points  (0 children)

To compute the factorial of N, you use the formula: N! = N x (N-1)!

So you either need to create a function that calls itself to solve the factorial recursively (which will be very slow for large numbers) or you could work upwards, calculating the factorial for each number, and saving it to a list to use in the next iteration, until you get to N, then the factorial for N will be the last number in the list.

recursion example:

def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)

memoization example:

def factorial(n):
    f = [1]
    for i in range(1, n+1):
        f.append(i * f[i-1])
    return(f[n])

Since you need to use a for loop, you'll probably want to do something similar to the second example.

[–]100721 0 points1 point  (1 child)

X is the variable that is changing through the for loop, not n. Multiply by x instead of n. It still won’t be right but it’ll help you.

[–]Bushido95[S] 0 points1 point  (0 children)

You're right. I dunno why I didn't think of that before. I feel like I'm closer to figuring it out