Last active
February 27, 2018 11:22
-
-
Save komasaru/9369b9dee75e5f23a1172555723659f3 to your computer and use it in GitHub Desktop.
Python script to compute factorials.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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