Sunday, October 16, 2011

Calculate Factorial in python

Objective : Using python calculate factorial of a number.
Logic : the logic is exactly the same as we do it in C language. so we use loops.

Code:
def factorial(number):

    ans=1;

    for i in range(2,number+1):

        ans=ans*i;

    print(ans)

    return

What are the benefits of this code in python over the same in C? well on the first you won't see much advantages, but that's where you overlook Python !!! Python utilizes a heavy bit integers (128 bit integers), so 200! can be easily calculated without using decimal representation. Isn't that cool !!!

No comments:

Post a Comment