N ! 

Fast Factorial Functions

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

  • The algorithm SplitRecursive, because it is simple and the fastest algorithm which does not use prime factorization.
  • 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 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 computer language and is even fast up to 10000!.
  • The ParallelPrimeSwing algorithm, which is the PrimeSwing algorithm with improved performance using methods of concurrent programming and thus taking advantage of multiple core processors.
  • 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.
 Content  Link    
Algorithms    
Source Code    
Benchmarks    
Conclusions    
Download    
Approximations  X 
Bounds    
Factorial/Gamma    
Hadamard  X 
History    
Notation    
Binary Split    
Double Factorial  ‼ 
Prime Factorial    
Bibliography    
Bernoulli /
Euler
 X 
Binomial    
Variations    
Stieltjes' CF  X 
al-Haytham /
Lagrange
   
Factorial Digits    
Calculator    
RPN-Factorial    
Permutations    
Gamma
LogGamma
   

Fast-Factorial-Functions: The Homepage of Factorial Algorithms. (C) Peter Luschny, 2000-2010.
All information and all source code in this directory is free under the Creative Commons Attribution-ShareAlike 3.0 Unported License (the same license which Wikipedia uses). This page is listed on the famous "Dictionary of Algorithms and Data Structures" at the National Institute of Standards and Technology's web site (NIST). Apr. 2003 / Apr. 2009 : 140,000 visitors! Thank you!
 
Postscript: The best algorithms to compute the factorial function are based on the swinging factorial numbers. More on these numbers can be found on here: