lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.37k stars 157 forks source link

Implemented `math.factorial(n)` for calculating very large factorials with speed and accuracy. #2557

Closed kmr-srbh closed 4 months ago

kmr-srbh commented 4 months ago

Overview

Screenshot from 2024-02-27 23-12-57

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