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