Rand Stats

Math::Factorial

zef:ash

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

routinedoes
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!

  1. 0! to 20! come from a table.
  2. 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.
  3. On Raku++, the engine's own [*] 1..n does exactly this in C++.

Multiplying two big numbers (Raku++)

A limb is one base-10⁹ digit: nine decimal digits in a 32-bit integer.

The cache (:cache)

How each engine handles [*] 1..n

What the module does on each

Milliseconds, factorial($n) / the engine's own [*] 1..$n:

Raku++Raku++ 5.11Rakudomutsu
10000!2.5– / 1.78.8 / 362.0 / 6.4
100000!28– / 10361 / 1,91338 / 767
300000!126– / 657283 / –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:

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++Rakudomutsu
300000! again0.00.10.1
301000!13167
299900!77276193

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:

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.