Skip to content

Instantly share code, notes, and snippets.

@komasaru komasaru/calc_factorial.py
Last active Feb 27, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.