Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active February 27, 2018 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/9369b9dee75e5f23a1172555723659f3 to your computer and use it in GitHub Desktop.
Save komasaru/9369b9dee75e5f23a1172555723659f3 to your computer and use it in GitHub Desktop.
Python script to compute factorials.
#! /usr/local/bin/python3.6
"""
Computation of factorials
"""
import sys
import traceback
class CalcFactorial:
L = 64 # Digits of computation
L2 = int((L + 7 ) / 8) # Length of list
N = 49 # Number of computation
def compute(self):
""" Computation of factorials """
try:
s = [0 for _ in range(self.L2)]
s[-1] = 1
for k in range(1, self.N + 1):
s = self.__long_mul(s, k)
self.__display(k, s)
except Exception as e:
raise
def __long_mul(self, a, b):
""" Computation of long * short
:param list a
:param list b
:return list z
"""
try:
z = [0 for _ in range(self.L2)]
carry = 0
for i in reversed(range(self.L2)):
w = a[i]
z[i] = (w * b + carry) % 100000000
carry = int((w * b + carry) / 100000000)
return z
except Exception as e:
raise
def __display(self, k, s):
""" Display
:param int k
:param list s
"""
try:
print("{:2d}!=".format(k), end="")
for i in range(0, self.L2):
print("{:08d}".format(s[i]), end="")
print()
except Exception as e:
raise
if __name__ == '__main__':
try:
obj = CalcFactorial()
obj.compute()
except Exception as e:
traceback.print_exc()
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment