Example: factorial
Factorial with memoization.
import { Decimal, DecimalConstants, MathContext } from '@phensley/cldr';
type Cache = { [x: number]: Decimal };
class Factorial {
private cache: Cache = { 1: DecimalConstants.ONE };
constructor(private mc: MathContext) {}
calculate(n: number): Decimal {
let r = this.cache[n];
if (r) {
return r;
}
r = new Decimal(n);
if (n > 2) {
r = r.multiply(this.calculate(n - 1), this.mc);
}
this.cache[n] = r;
return r;
}
}
const reverse = (s: string) =>
s.split('').reverse().join('');
const spacer = (s: string): string =>
s.replace(/\w{5}|\w+/g, (m) => ` ${m}`).trim();
const fac = new Factorial({ precision: 100 });
const results: [number, string][] = [];
let width = 0;
for (let n = 1; n <= 50; n++) {
const tmp = fac.calculate(n).toString();
const result = reverse(spacer(reverse(tmp)));
width = Math.max(result.length, width);
results.push([n, result]);
}
for (const result of results) {
const [ n, s ] = result;
log(`${' '.repeat(width - s.length)}${s} = ${n}!`);
}
1 = 1!
2 = 2!
6 = 3!
24 = 4!
120 = 5!
720 = 6!
5040 = 7!
40320 = 8!
3 62880 = 9!
36 28800 = 10!
399 16800 = 11!
4790 01600 = 12!
62270 20800 = 13!
8 71782 91200 = 14!
130 76743 68000 = 15!
2092 27898 88000 = 16!
35568 74280 96000 = 17!
6 40237 37057 28000 = 18!
121 64510 04088 32000 = 19!
2432 90200 81766 40000 = 20!
51090 94217 17094 40000 = 21!
11 24000 72777 76076 80000 = 22!
258 52016 73888 49766 40000 = 23!
6204 48401 73323 94393 60000 = 24!
1 55112 10043 33098 59840 00000 = 25!
40 32914 61126 60563 55840 00000 = 26!
1088 88694 50418 35216 07680 00000 = 27!
30488 83446 11713 86050 15040 00000 = 28!
8 84176 19937 39701 95454 36160 00000 = 29!
265 25285 98121 91058 63630 84800 00000 = 30!
8222 83865 41779 22817 72556 28800 00000 = 31!
2 63130 83693 36935 30167 21801 21600 00000 = 32!
86 83317 61881 18864 95518 19440 12800 00000 = 33!
2952 32799 03960 41408 47618 60964 35200 00000 = 34!
1 03331 47966 38614 49296 66651 33752 32000 00000 = 35!
37 19933 26789 90121 74679 99448 15083 52000 00000 = 36!
1376 37530 91226 34504 63159 79581 58090 24000 00000 = 37!
52302 26174 66601 11176 00072 24100 07429 12000 00000 = 38!
20 39788 20811 97443 35864 02817 39902 89735 68000 00000 = 39!
815 91528 32478 97734 34561 12695 96115 89427 20000 00000 = 40!
33452 52661 31638 07108 17006 20534 40751 66515 20000 00000 = 41!
14 05006 11775 28798 98543 14260 62445 11569 93638 40000 00000 = 42!
604 15263 06337 38356 37355 13206 85139 97507 26451 20000 00000 = 43!
26582 71574 78844 87680 43625 81101 46158 90319 63852 80000 00000 = 44!
11 96222 20865 48019 45619 63161 49565 77150 64383 73376 00000 00000 = 45!
550 26221 59812 08894 98503 05428 80025 48929 61651 75296 00000 00000 = 46!
25862 32415 11168 18064 29643 55153 61197 99691 97632 38912 00000 00000 = 47!
12 41391 55925 36072 67086 22890 47373 37503 85214 86354 67776 00000 00000 = 48!
608 28186 40342 67560 87225 21633 21295 37688 75528 31379 21024 00000 00000 = 49!
30414 09320 17133 78043 61260 81660 64768 84437 76415 68960 51200 00000 00000 = 50!