Factorials of big numbers

Usage:

import fact
fact.factorial(1000)

Code:

import math

'''
Checks for factorial of n in factorials dictionary and
returns if it is, else it checks the difference between
 the highest number of factorial   in factorials dictionary
and n. If the difference is greater than the limit
 (default is 500) factorial is calculated for n in steps.
In python the default recursion depth is 1000.
'''

factorials = {0:1,1:1}

def factorial(n,limit=500):
    if factorials.has_key(n):
        return factorials[n]
    lastkey = sorted(factorials.keys())[-1]
    difference = n-lastkey
    if  difference < limit:
        return fact(n)
    else:
        incrementIndex =  int(math.ceil(difference/float(limit)))
        bigfactorial = 0
        for i in range(1,incrementIndex+1):
            currentN  =   lastkey + limit * i
            fact(currentN)
        return factorials[n]

''' Recursive function for  factorial '''
def fact(n):
    if factorials.has_key(n):
        return factorials[n]
    else:
        t = n * fact(n-1)
        factorials[n] = t
        return t

Other articles with the same tag

  • Python datetime snippets
  • Alternate Small Caps Text Using Angular
  • Debugging Flask app with pdb
  • Brand icons using Font-Awesome
  • Python tidbits
  • Prime class to find nth prime
  • Binary to Text Script
  • Back