mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Add factorialRecursiveTCOBigInt
This commit is contained in:
parent
9787af0267
commit
2b4f12622a
@ -0,0 +1,12 @@
|
||||
import factorialRecursiveTCOBigInt from '../factorialRecursiveTCOBigInt';
|
||||
|
||||
describe('factorialRecursiveTCOBigInt', () => {
|
||||
it('should calculate factorial', () => {
|
||||
expect(factorialRecursiveTCOBigInt(0n)).toBe(1n);
|
||||
expect(factorialRecursiveTCOBigInt(1n)).toBe(1n);
|
||||
expect(factorialRecursiveTCOBigInt(5n)).toBe(120n);
|
||||
expect(factorialRecursiveTCOBigInt(8n)).toBe(40320n);
|
||||
expect(factorialRecursiveTCOBigInt(10n)).toBe(3628800n);
|
||||
expect(factorialRecursiveTCOBigInt(100n)).toBe(93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000n);
|
||||
});
|
||||
});
|
12
src/algorithms/math/factorial/factorialRecursiveTCOBigInt.js
Normal file
12
src/algorithms/math/factorial/factorialRecursiveTCOBigInt.js
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @param {bigint} number
|
||||
* @return {bigint}
|
||||
*/
|
||||
export default function factorialRecursiveTCOBigInt(number) {
|
||||
function fact(number, accumulator = 1n) {
|
||||
if (number < 2n) return accumulator;
|
||||
else return fact(number - 1n, accumulator * number);
|
||||
}
|
||||
|
||||
return fact(number);
|
||||
}
|
Loading…
Reference in New Issue
Block a user