Math::Factorial
Exact factorials, fast on every engine, with a cache you can ask for.
Version
0.0.1
Synopsis
use Math::Factorial;
say factorial(20); # 2432902008176640000
say factorial(100_000).chars; # 456574
factorial(50_000, :cache); # computed and kept
factorial(50_010, :cache); # 50000! times ten numbers
factorial-cache-clear(); # let the memory go
say factorial-backend(); # 'core' on Raku++, 'raku' elsewhere
The interface
| routine | does |
|---|
factorial(Int:D $n) | $n! as an Int; a negative $n fails with X::OutOfRange |
factorial(Int:D $n, :cache) | the same, kept, and built from what is already kept |
factorial-cache-clear() | empties the cache |
factorial-backend() | 'core' (the engine's [*]) or 'raku' (this module's tree) |
The algorithm, briefly
Computing n!
0! to 20! come from a table.- Otherwise, split
1..n at the middle, compute each half's product the same
way, and multiply the two results. Below a few dozen numbers, just multiply
in a loop. - On Raku++, the engine's own
[*] 1..n does exactly this in C++.
Multiplying two big numbers (Raku++)
- Small: schoolbook, digit by digit.
- Medium: Karatsuba. Split each number in half and use three half-size
multiplications instead of four.
- Large, from 1,500 limbs (about 13,500 digits): number-theoretic transform.
Treat the limbs as polynomial coefficients, transform both, multiply point
by point, and transform back. This is done modulo three primes, and the
results are recombined with the Chinese remainder theorem.
A limb is one base-10⁹ digit: nine decimal digits in a 32-bit integer.
The cache (:cache)
- Keep every answer.
- To find a new
n!, start from the nearest kept m! below and multiply by
(m+1)⋯n. - On Raku++, if a kept
M! is much closer above, divide it by (n+1)⋯M
instead.
How each engine handles [*] 1..n
- Raku++: recognises
[*] over an integer range and computes it with the
product tree in C++, using its fast multiply (with the transform for large
numbers). It is the fastest of the three. - Rakudo: folds left to right,
((1·2)·3)·4…, so the cost is quadratic:
100000! takes about 1.9 s. Printing the result is slower still, because
Rakudo stores big numbers in binary and converting to decimal is slow (53 s
for 100000!). - mutsu: also folds left to right. It is fast for small
n (6 ms for
10000!) but quadratic for large n (0.77 s for 100000!, 7.5 s for
300000!).
What the module does on each
- Raku++: hands the range to
[*]. - Rakudo: builds the product tree itself, with a Raku loop at the leaves.
Rakudo's
[*] is slow even over a short range. - mutsu: builds the product tree itself, with
[*] at the leaves of up to
256 numbers, where mutsu's own fold is fast. - Any other engine: the tree with loop leaves, which is correct everywhere
and never quadratic.
Milliseconds, factorial($n) / the engine's own [*] 1..$n:
| Raku++ | Raku++ 5.11 | Rakudo | mutsu |
|---|
10000! | 2.5 | – / 1.7 | 8.8 / 36 | 2.0 / 6.4 |
100000! | 28 | – / 103 | 61 / 1,913 | 38 / 767 |
300000! | 126 | – / 657 | 283 / – | 195 / 7,548 |
On Raku++ the module calls [*], so the two are one number. Raku++ is
af2bf7b6, Rakudo 2026.11, mutsu 2026.11 (1.0.0, d57da8e); measured
2026-12-06 on arm64 with tools/bench.raku.
N.B. Time the computation, not say. On Rakudo, say [*] 1..100000 takes 56 s,
and 53 s of that is turning the 456,574-digit result into text (.Str).
Raku++ does that step in 6 ms, because it stores big integers in decimal.
The cache
factorial($n, :cache) keeps its answer, and builds it from the nearest
answer already kept instead of from 1:
- from the nearest one below,
m!, as m! × (m+1)⋯n; - from the nearest one above,
M!, as M! ÷ (n+1)⋯M, on Raku++ only and
only when M is much closer — under a sixty-fourth of the distance to m
and a thousandth of n. A division costs far more than a multiplication.
On Rakudo dividing a big Int costs about the same whatever it is divided
by — 3 s for 300000!, ten times computing it — so there it is never taken.
Nor on mutsu, or any engine not measured: only Raku++ divides.
A call without :cache neither reads the cache nor adds to it. That is the
default because a factorial is big: 100000! is 190 kB, and what is kept
stays until factorial-cache-clear() empties the cache.
Milliseconds, with 300000! held:
| Raku++ | Rakudo | mutsu |
|---|
300000! again | 0.0 | 0.1 | 0.1 |
301000! | 13 | 16 | 7 |
299900! | 77 | 276 | 193 |
299900! is a division on Raku++ and computed afresh on the other two.
Lookups and insertions are under a lock, so threads can share the cache; the
multiplications run outside it, and two threads asking for the same new n
at once may both compute it, one of them keeping it.
Backends
factorial-backend() says which multiplies:
core: the engine's own [*]. The module uses it only on an engine
known to multiply a range as a product tree. Today that list is Raku++
alone (@TREE-REDUCE in the module).raku: the module's own tree, on every other engine. That is Rakudo,
mutsu, and any engine nobody has measured yet. Rakudo and mutsu both fold
[*] left to right (mutsu takes 0.77 s for [*] 1..100000, the module
38 ms), and the tree is correct everywhere and never quadratic, so an
unknown engine is safe with it.
MATH_FACTORIAL_BACKEND=raku or =core forces either one on any
engine. Use it to compare the two, or to try a new engine before it goes on
the list. An engine belongs on the list once its [*] 1..100000 beats the
module's tree on that engine. The suite checks the engine's answer against
the tree wherever core is in use.
rakudo -Ilib tools/bench.raku
raku++ -Ilib tools/bench.raku
mutsu -Ilib tools/bench.raku
MATH_FACTORIAL_BACKEND=raku raku++ -Ilib tools/bench.raku
On a Raku++ released before 5.12 [*] over a range is a left-to-right fold
again, and 'core' is slower than 'raku'; the answers are the same.
Scope
Integers only: no Gamma function, no postfix:<!>, no double or multi
factorials, no binomials.