AlexBrand
AlexBrand

Reputation: 12399

calculating factorial in Python

When calculating

math.factorial(100)

I get:

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L

Why is there an L at the end of the number?

Upvotes: 1

Views: 9389

Answers (5)

Nishant Chaudhary
Nishant Chaudhary

Reputation: 1

Python factorial using for loop

def fact(n):
  if n<=1:
    print("Enter a positive no.")
  else:
    for i in range(n,1,-1):
      n = n*(i-1)
    return n
print(fact(int(input("Enter a number : "))))

Upvotes: 0

Eric He
Eric He

Reputation: 11

you could use the factorial calculator code...

def factorial (number):
    product=1;
    for i in range(number):
        product=product*(i+1)
    return product

I do not have Python, so I did not test out the code. I guarantee that this function will work.

Upvotes: 0

Hamish Grubijan
Hamish Grubijan

Reputation: 10820

I believe that you are working with a BigInt, which is known as long in Python - it expands and occupies a variable amount of RAM as needed. The name long can be confusing, as that means a specific number of bytes in a handful of popular languages of today. The following can help you get at the number of bytes it takes to store the object.

Python 2.6.2 (r262:71600, Aug 14 2009, 22:02:40) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import sys
>>> a = 1
>>> sys.getsizeof(a)
12
>>> import math
>>> sys.getsizeof(math.factorial(100))
84
>>> sys.getsizeof(math.factorial(200))
182
>>> 

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304137

L means it's a long as opposed to an int. The reason you see it is that you are looking at the repr of the long

You can use

print math.factorial(100)

or

str(math.factorial(100))

if you just want the number

Upvotes: 15

Nathan Fellman
Nathan Fellman

Reputation: 127428

The L means that it's a long integer

Upvotes: 3

Related Questions