Utilize cached-expression to calculate multiple factorial numbers
import CachedFactorial from 'cached-factorial'
const { calculate } = new CachedFactorial()
const result = [0, 1, 2, 3, 4, 5].map(calculate) // expect: [1, 1, 2, 6, 24, 120]
This is only useful when you have to calculate factorials of more than on number.
Let's say you have to calculate 5!
and 7!
. That gives us:
5! = 5 * 4 * 3 * 2 * 1
7! = 7 * 6 * (5 * 4 * 3 * 2 * 1) = 7 * 6 * 5!
If you were to calculate 5!
and 7!
independently, 5!
(and by extension, 4!
, 3!
, 2!
) will be called twice. This is wasteful!
In order to avoid this wasteful operation, CachedFactorial
stores every factorial that is calculated in a cache so that every factorial is only computed once.