From c5ed81d85e2c131df3112a5a02d7946786d5c190 Mon Sep 17 00:00:00 2001 From: m-maksyutin Date: Thu, 28 Jun 2018 21:03:31 +0300 Subject: [PATCH] Add recursive factorial function (#85) * Fix LinkedList * Fix the prepend method for the LinkedList * Fix the remove method for the MinHeap * Correct a comment * Fix BST removal method * Fix the findEdge method of the graph * Fix the value returned by DisjointSet union * Add recursive factorial function --- .../factorial/__test__/factorialRecursive.test.js | 11 +++++++++++ src/algorithms/math/factorial/factorialRecursive.js | 7 +++++++ 2 files changed, 18 insertions(+) create mode 100644 src/algorithms/math/factorial/__test__/factorialRecursive.test.js create mode 100644 src/algorithms/math/factorial/factorialRecursive.js diff --git a/src/algorithms/math/factorial/__test__/factorialRecursive.test.js b/src/algorithms/math/factorial/__test__/factorialRecursive.test.js new file mode 100644 index 00000000..9029faee --- /dev/null +++ b/src/algorithms/math/factorial/__test__/factorialRecursive.test.js @@ -0,0 +1,11 @@ +import factorialRecursive from '../factorialRecursive'; + +describe('factorialRecursive', () => { + it('should calculate factorial', () => { + expect(factorialRecursive(0)).toBe(1); + expect(factorialRecursive(1)).toBe(1); + expect(factorialRecursive(5)).toBe(120); + expect(factorialRecursive(8)).toBe(40320); + expect(factorialRecursive(10)).toBe(3628800); + }); +}); diff --git a/src/algorithms/math/factorial/factorialRecursive.js b/src/algorithms/math/factorial/factorialRecursive.js new file mode 100644 index 00000000..e2b4aec6 --- /dev/null +++ b/src/algorithms/math/factorial/factorialRecursive.js @@ -0,0 +1,7 @@ +/** + * @param {number} number + * @return {number} + */ +export default function factorialRecursive(number) { + return number > 1 ? number * factorialRecursive(number - 1) : 1; +}