{"slug": "fast-factorial-algorithms", "title": "Fast Factorial Algorithms", "summary": "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.", "body_md": "There are five algorithms which everyone who wants to compute the factorial n! = 1.2.3...n should know.\nBigInt 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); }\nAn example of a PrimeSwing computation:\nAs 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.\nA concise description of this algorithm is given in this write-up (pdf) and in the SageMath link below (Algo 5).\nFast-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!", "url": "https://wpnews.pro/news/fast-factorial-algorithms", "canonical_source": "http://www.luschny.de/math/factorial/FastFactorialFunctions.htm", "published_at": "2026-05-20 03:55:58+00:00", "updated_at": "2026-05-23 11:36:04.952554+00:00", "lang": "en", "topics": ["research"], "entities": ["Peter Luschny", "NIST", "SageMath", "PrimeSwing"], "alternates": {"html": "https://wpnews.pro/news/fast-factorial-algorithms", "markdown": "https://wpnews.pro/news/fast-factorial-algorithms.md", "text": "https://wpnews.pro/news/fast-factorial-algorithms.txt", "jsonld": "https://wpnews.pro/news/fast-factorial-algorithms.jsonld"}}