cached-factorial

Utilize cached-expression to calculate multiple factorial numbers

Usage no npm install needed!

<script type="module">
  import cachedFactorial from 'https://cdn.skypack.dev/cached-factorial';
</script>

README

cached-factorial

Utilize cached-expression to calculate multiple factorial numbers

Usage

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]

Use Case: Factorials of Multiple Numbers

This is only useful when you have to calculate factorials of more than on number.

Why and How?

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.

License

MIT © Hoàng Văn Khải