Why python is not able find the factorial of such numbers?


#1
def factorial(x):
    if x < 0:
        print("not a valid num")
    elif x == 0:
        print("factorial of 0 is 1")
    else:
        for i in range(1, x):
            x = x * i
        print(x)


factorial(456998)

#2

you change the value of x, this means your loop ends to early (given the factorial is bigger then the number itself, your loop ends to early)

add some print statement to see the problem in action


#3

i didnt get you?where should i add the print statement


#4

well, i said the value of x is increasing to rapid which will cause your loop to end too soon, so a print statement in the for loop to display the value of x. Ideally, you would figure out where to insert the print statement to better see and understand your program

you use x for both the loop condition and to store the factorial result, this is conflicting


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.