From 4ea1ad0bb8a8d62c433b1bab3df3596ad23c0b12 Mon Sep 17 00:00:00 2001 From: trainer2001 <95953188+trainer2001@users.noreply.github.com> Date: Sun, 12 Dec 2021 15:39:32 +0530 Subject: [PATCH 1/3] Add factorialRecursiveTCO --- .../math/factorial/factorialRecursiveTCO.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/algorithms/math/factorial/factorialRecursiveTCO.js diff --git a/src/algorithms/math/factorial/factorialRecursiveTCO.js b/src/algorithms/math/factorial/factorialRecursiveTCO.js new file mode 100644 index 00000000..804b8202 --- /dev/null +++ b/src/algorithms/math/factorial/factorialRecursiveTCO.js @@ -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); +} From 9787af026702ea1311d67af1e4d50ec06d637533 Mon Sep 17 00:00:00 2001 From: trainer2001 <95953188+trainer2001@users.noreply.github.com> Date: Sun, 12 Dec 2021 15:40:21 +0530 Subject: [PATCH 2/3] Add test for factorialRecursiveTCO --- .../factorial/__test__/factorialRecursiveTCO.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/algorithms/math/factorial/__test__/factorialRecursiveTCO.test.js diff --git a/src/algorithms/math/factorial/__test__/factorialRecursiveTCO.test.js b/src/algorithms/math/factorial/__test__/factorialRecursiveTCO.test.js new file mode 100644 index 00000000..e379e6a0 --- /dev/null +++ b/src/algorithms/math/factorial/__test__/factorialRecursiveTCO.test.js @@ -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); + }); +}); From 2b4f12622ad49506f4cdcf1a36d9427569349177 Mon Sep 17 00:00:00 2001 From: trainer2001 <95953188+trainer2001@users.noreply.github.com> Date: Sun, 12 Dec 2021 16:59:56 +0530 Subject: [PATCH 3/3] Add factorialRecursiveTCOBigInt --- .../__test__/factorialRecursiveTCOBigInt.test.js | 12 ++++++++++++ .../math/factorial/factorialRecursiveTCOBigInt.js | 12 ++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/algorithms/math/factorial/__test__/factorialRecursiveTCOBigInt.test.js create mode 100644 src/algorithms/math/factorial/factorialRecursiveTCOBigInt.js diff --git a/src/algorithms/math/factorial/__test__/factorialRecursiveTCOBigInt.test.js b/src/algorithms/math/factorial/__test__/factorialRecursiveTCOBigInt.test.js new file mode 100644 index 00000000..ab775b38 --- /dev/null +++ b/src/algorithms/math/factorial/__test__/factorialRecursiveTCOBigInt.test.js @@ -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); + }); +}); diff --git a/src/algorithms/math/factorial/factorialRecursiveTCOBigInt.js b/src/algorithms/math/factorial/factorialRecursiveTCOBigInt.js new file mode 100644 index 00000000..abccb07c --- /dev/null +++ b/src/algorithms/math/factorial/factorialRecursiveTCOBigInt.js @@ -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); +}