N ! 

Fast Factorial Functions

There are four algorithms which everyone who wants to compute the factorial n! = 1.2.3...n exactly should know.

  • The algorithm PrimeSwing, because it is the (asymptotical) fastest algorithm known to compute n!. The algorithm is based on the notion of the 'Swing Numbers' and computes n! via the prime factorization of these numbers.
  • The algorithm SplitRecursive, because it is the fastest algorithm not using prime factorization (and thereby being considerably simpler to implement than the algorithm 'Prime-Swing').
  • The ingenious algorithm of Moessner which uses only additions! Though of no practical importance (because it is slow), it has the fascination of an unexpected solution.
  • The Poor Man's algorithm which uses no Big-Integer library and can be easily implemented in any language and is even fast up to 10000!
  • And here is an algorithm which nobody needs, for the Simple-Minded only:
    long factorial(long n) { return n <= 1 ? 1 : n * factorial(n-1); } Do not use it if n > 12.

 Page  Link
Algorithms
Source Code
Benchmarks
Conclusions
Download
Approximations
Bounds
Factorial/Gamma
Hadamard
History
Binary Split
Prime Factorial
Bibliography
Bernoulli/Euler
Binomial
Variations
Continued Fraction
Calculate n! for n up to incredible 9.999.999.999 . Calculator


FastFactorialFunctions: The Homepage of Factorial Algorithms. (C) Peter Luschny 2000-2008
This page is listed in the famous "Dictionary of Algorithms and Data Structures" on the National Institute of Standards and Technology's web site (NIST). Apr.13,2003 - Jan.1,2008: 90,000 visitors! Thank you!

Webstats4U   previous
Visit Peter's math pages.
Valid XHTML 1.1