Python can handle Very Large Numbers

vegaseat 2 Tallied Votes 5K Views Share

Scientists and deficit spenders like to use Python because it can handle very large numbers. I decided to give it a test with factorials. Factorials reach astronomical levels rather quickly. In case you can't quite remember, the factorial of 12 is !12 = 1*2*3*4*5*6*7*8*9*10*11*12 = 479001600, that is 479 million and some change! After that languages like C++ start to fizzle quickly! At !16 we get past the nations debt. Python can handle that and more! I stopped at !69 only because the display started to wrap the line. The result has 98 digits in it, I thought I made my point anyway.

# check the numeric range of Python with huge factorials
# factorial(69) still checks out accurately! Has 98 digits in it!
# 171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000
# tested with Python24      vegaseat    01aug2005

def getFactorial(n):
    """returns the factorial of n"""
    if n == 0:
        return 1
        k = n * getFactorial(n-1)
        return k

for k in range(1, 70):
    print "factorial of", k,"=", getFactorial(k)
mlin 0 Newbie Poster

I didn't know Python could handle such big numbers natively. Cool stuff, particularly for cryptography I'm sure. I hit a wall at 999.

>>> getFactorial(999)
  File "<stdin>", line 5, in g
  File "<stdin>", line 5, in g
  File "<stdin>", line 5, in g
RuntimeError: maximum recursion depth exceeded

Try this definition instead. It seems to be getting the same results as yours, but is non-recursive.

>>> def getFactorial(n):
...   r = 1
...   for x in range(n):
...     r = r * (n + 1)
...   return r
>>> getFactorial(100000)
(insert monster number here)L
ultimatebuster 14 Posting Whiz in Training

i know this for a long time. lol

I think i had something x 10^500 or 10^5000 at one point.

Ene Uran 638 Posting Virtuoso

There is a limit to recursions in Python,
find the limit with
sys.getrecursionlimit() --> usually 1000
Can be changed with

Lucaci Andrew 140 Za s|n

You could use an iterative version of the factorial for big numbers:

>>> factorial(1000)

Here's the version:

>>> def factorial(nr, prod = 1):
    for i in range(nr): prod *= (i + 1)
    return prod

[Edit:] Nvm, already posted this above, didn't see it.
[Edit2:] Nvm the first edit, the factorial function provided above is wrong:

def getFactorial(n):
    r = 1
    for x in range(n):
        r = r * (n + 1)
    return r

it should be

def getFactorial(n):
    r = 1
    for x in range (n):
        r = r * (x+1)
    return r
paddy3118 11 Light Poster

If you want even larger numbers from a simple series then the Ackermann function is to factorials as factorials are to a simple count of integers.

sneekula 969 Nearly a Posting Maven

The factorial function is part of the math module in later versions of Python:

tested with Python 2.7.3

from math import factorial

for k in range(1, 101):
    print("factorial of {} = {}".format(k, factorial(k)))

print(type(factorial(10)))       # <type 'int'>
print(type(factorial(100)))      # <type 'long'>
print(len(str(factorial(100))))  # 158
print(len(str(factorial(69))))   # 99
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.