The current implementation of math.factorial() is naive.
Returns 0 for factorial for large values due to integer overflow.
Solution
The industry standard for calculating factorial of a large number is complicated. It deals with Prime Number generation and Swing Numbers. These methods require BigInt for storing the large calculated value.
One fast method for going about calculating the calculation is to use an array to store the digits of the number as strings, in reverse, and do digit by digit multiplication. This method is used for implementing the algorithm.
I support the implementation of the Swing Number algorithms if one can understand and implement it correctly. The funny thing is that a Python implementation is available online.
Improvement
Works for large factorials
Works for very large factorials
Accurate
Integration tests have not been added due to the prerequisite below.
Prerequisite
While the function is complete, it depends on #2554 for getting merged. Currently it just prints the output to stdout and returns 0.
The long assignment of digits is because LPython currently does not support list assignment like: my_list[0:4] = [1, 2, 3]. The assignments can be improved through the introduction of the above list assignment.
Overview
math.factorial()
is naive.Solution
The industry standard for calculating factorial of a large number is complicated. It deals with Prime Number generation and Swing Numbers. These methods require
BigInt
for storing the large calculated value.One fast method for going about calculating the calculation is to use an array to store the digits of the number as strings, in reverse, and do digit by digit multiplication. This method is used for implementing the algorithm.
I support the implementation of the Swing Number algorithms if one can understand and implement it correctly. The funny thing is that a Python implementation is available online.
Improvement
Integration tests have not been added due to the prerequisite below.
Prerequisite
my_list[0:4] = [1, 2, 3]
. The assignments can be improved through the introduction of the above list assignment.