cd /news/research/fast-factorial-algorithms · home topics research article
[ARTICLE · art-11258] src=luschny.de ↗ pub= topic=research verified=true sentiment=· neutral

Fast Factorial Algorithms

The article describes five algorithms for computing factorials, including a recursive "product tree" method and the "PrimeSwing" algorithm, which relies on the prime factorization of swinging factorials for efficiency. It notes that the factorial computation can be reduced to computing the swinging factorial. The page, authored by Peter Luschny, is listed in NIST's Dictionary of Algorithms and Data Structures and is freely available under a Creative Commons license.

read1 min views20 publishedMay 20, 2026

There are five algorithms which everyone who wants to compute the factorial n! = 1.2.3...n should know. BigInt recfact(long start, long n) { long i; if (n <= 16) { BigInt r = new BigInt(start); for (i = start + 1; i < start + n; i++) r *= i; return r; } i = n / 2; return recfact(start, i) * recfact(start + i, n - i); } BigInt factorial(long n) { return recfact(1, n); } An example of a PrimeSwing computation: As this example shows an efficient computation of the factorial function reduces to an efficient computation of the swinging factorial n≀. Some information about these numbers can be found here and here. The prime factorization of the swing numbers is crucial for the implementation of the PrimeSwing algorithm. A concise description of this algorithm is given in this write-up (pdf) and in the SageMath link below (Algo 5). Fast-Factorial-Functions: The Homepage of Factorial Algorithms. (C) Peter Luschny, 2000-2017. All information and all source code in this directory is free under the Creative Commons Attribution-ShareAlike 3.0 Unported License. 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. 2017 : 800,000 visitors! Thank you!

── more in #research 4 stories · sorted by recency
── more on @peter luschny 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/fast-factorial-algor…] indexed:0 read:1min 2026-05-20 ·