Fast Factorial Functions
Project status: maintained and up-to-date.
Here you can find implementations of the most efficient algorithms to compute
- the factorial function,
- the double factorial function,
- the binomial function.
See also this nice overview.
Short guide:
-
If you want to study the different algorithms proposed to compute the factorial function the best start is to look into this directory.
-
If you just want to study/use the fastest algorithm the best start probably is to read the SageMath implementation or the Python implementation or the Julia implementation of the prime swing algorithm.
Implementation languages
- Project MpirBasedFunctions : C++
- Project SilverFactorial64 : C#
- Project JavaFactorial : Java
- Project GoFactorial : Go
- Project SageMathFactorial : SageMath
- Project PyFactorial : Python
- Project JFactorial : Julia
The C# and the Java version come with a small benchmark program. Here a screenshot of the Java version and a screenshot of the C# version.
Browsing the code
To browse the code the following two pages might be more convenient: primes and factorials.
Porting
If you want to port the algorithms to other languages the C# version is recommended as the point of departure. The benchmarks indicate that it is a good idea to start with the swing algorithm or (more demanding to implement, but at least twice as fast) the prime swing algorithm.
A good starting point for the binomial function is here.
Dependencies
To build the sources you need for
-
the C++ version the MPIR library.
-
The C# version uses the MPIR library (an interop is provided).
-
The Java version needs Mikko Tommila's Apfloat library. If you want to compile the benchmark program additionally Karsten Lentzsch's JGoodies is needed.
Contributing
Please notify me of any bugs. Want to contribute new algorithms? Great, please contact me. If you already ported to some other language (Scala, F#, Rust, Ruby, Lisp) then please send me your code so I can incorporate it into this repository.
Contributors
Sonia Keys ported the algorithms to Go.
Michael Saupe contributed a performance optimized python3 version with gmpy2.