From 80b2c8eb070581c64a0e0c196749f3ef693f23c0 Mon Sep 17 00:00:00 2001 From: Eiden0 Date: Wed, 1 Sep 2021 02:12:01 +0530 Subject: [PATCH 1/2] added get Divisors and Fisher Yates Algorithm --- src/algorithms/math/divisors.js | 21 +++++++++++++++++++++ src/algorithms/math/fisher_Yates.js | 23 +++++++++++++++++++++++ src/algorithms/math/index.js | 4 ++++ test/algorithms/math/divisors.js | 25 +++++++++++++++++++++++++ test/algorithms/math/fisherYates.js | 15 +++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 src/algorithms/math/divisors.js create mode 100644 src/algorithms/math/fisher_Yates.js create mode 100644 test/algorithms/math/divisors.js create mode 100644 test/algorithms/math/fisherYates.js diff --git a/src/algorithms/math/divisors.js b/src/algorithms/math/divisors.js new file mode 100644 index 00000000..98bb6d5e --- /dev/null +++ b/src/algorithms/math/divisors.js @@ -0,0 +1,21 @@ +/** + * Calculates the list of divisors of a number in sorted order + * @param {Number} n number + * @return {Array} Array of Divisors + * Time Complexity = O(n ^ 0.5) + */ + const getDivisors = (n) => { + let divisors = []; + if (n < 1) return []; + const tempArray = []; + for (let i = 1; i * i <= n; i += 1) { + if (n % i === 0) { + divisors.push(i); + if (i !== n / i) tempArray.push(n / i); + } + } + divisors = divisors.concat(tempArray.reverse()); + return divisors; +}; + +module.exports = getDivisors; diff --git a/src/algorithms/math/fisher_Yates.js b/src/algorithms/math/fisher_Yates.js new file mode 100644 index 00000000..644130a5 --- /dev/null +++ b/src/algorithms/math/fisher_Yates.js @@ -0,0 +1,23 @@ +/** + * Shuffle array in-place using Fisher-Yates Algorithm + * @param {Array} array Array to be shuffled + * @return {Array} Shuffled Array + * + */ + const Shuffle = (array) => { + let currentIndex = array.length; + let randomIndex; + + while (currentIndex != 0) { + + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex -= 1; + + [array[currentIndex], array[randomIndex]] = [ + array[randomIndex], array[currentIndex]]; + } + + return array; +}; + +module.exports = Shuffle; \ No newline at end of file diff --git a/src/algorithms/math/index.js b/src/algorithms/math/index.js index 41b2e488..47995009 100644 --- a/src/algorithms/math/index.js +++ b/src/algorithms/math/index.js @@ -3,8 +3,12 @@ const gcd = require('./gcd'); const fastexp = require('./fast_exp'); const lcm = require('./lcm'); const modularInverse = require('./modular_inverse'); +const Shuffle = require('./fisher_Yates'); +const getDivisors = require('./divisors'); module.exports = { + Shuffle, + getDivisors, extendedEuclidean, gcd, fastexp, diff --git a/test/algorithms/math/divisors.js b/test/algorithms/math/divisors.js new file mode 100644 index 00000000..9710e197 --- /dev/null +++ b/test/algorithms/math/divisors.js @@ -0,0 +1,25 @@ +/* eslint-env mocha */ +const getDivisors = require('../../../src').algorithms.math.getDivisors; + +const assert = require('assert'); + +describe('Divisors', () => { + it('should return empty array for number 0', () => { + assert.deepEqual(getDivisors(0), []); + }); + + it('should return [] for negative numbers ', () => { + assert.deepEqual(getDivisors(-3), []); + }); + + it('should return [1] for 1', () => { + assert.deepEqual(getDivisors(1), [1]); + + }); + + it('should return divisors of a number', () => { + assert.deepEqual(getDivisors(100), [1, 2, 4, 5, 10, 20, 25, 50, 100]); + assert.deepEqual(getDivisors(10), [1, 2, 5, 10]); + assert.deepEqual(getDivisors(125), [1, 5, 25, 125]); + }); +}); \ No newline at end of file diff --git a/test/algorithms/math/fisherYates.js b/test/algorithms/math/fisherYates.js new file mode 100644 index 00000000..2d7adf68 --- /dev/null +++ b/test/algorithms/math/fisherYates.js @@ -0,0 +1,15 @@ +/* eslint-env mocha */ +const Shuffle = require('../../../src').algorithms.math.Shuffle; + +const assert = require('assert'); + +describe('Shuffle', () => { + it('should return empty array for an empty array', () => { + assert.deepEqual(Shuffle([]), []); + }); + + it('length of both the array should be same ', () => { + const arr = [1, 2, 3]; + assert.deepEqual(Shuffle(arr).length, 3); + }); +}); \ No newline at end of file From f188db68d2955a88f4a522dfd8512fc0b18b91be Mon Sep 17 00:00:00 2001 From: Eiden0 Date: Wed, 1 Sep 2021 02:26:06 +0530 Subject: [PATCH 2/2] prettied code --- src/algorithms/math/divisors.js | 2 +- src/algorithms/math/fisher_Yates.js | 7 +++---- test/algorithms/math/divisors.js | 3 +-- test/algorithms/math/fisherYates.js | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/algorithms/math/divisors.js b/src/algorithms/math/divisors.js index 98bb6d5e..e41d23bc 100644 --- a/src/algorithms/math/divisors.js +++ b/src/algorithms/math/divisors.js @@ -4,7 +4,7 @@ * @return {Array} Array of Divisors * Time Complexity = O(n ^ 0.5) */ - const getDivisors = (n) => { +const getDivisors = (n) => { let divisors = []; if (n < 1) return []; const tempArray = []; diff --git a/src/algorithms/math/fisher_Yates.js b/src/algorithms/math/fisher_Yates.js index 644130a5..d7239d4f 100644 --- a/src/algorithms/math/fisher_Yates.js +++ b/src/algorithms/math/fisher_Yates.js @@ -4,12 +4,11 @@ * @return {Array} Shuffled Array * */ - const Shuffle = (array) => { +const Shuffle = (array) => { let currentIndex = array.length; let randomIndex; - while (currentIndex != 0) { - + while (currentIndex !== 0) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; @@ -20,4 +19,4 @@ return array; }; -module.exports = Shuffle; \ No newline at end of file +module.exports = Shuffle; diff --git a/test/algorithms/math/divisors.js b/test/algorithms/math/divisors.js index 9710e197..f008c0db 100644 --- a/test/algorithms/math/divisors.js +++ b/test/algorithms/math/divisors.js @@ -14,7 +14,6 @@ describe('Divisors', () => { it('should return [1] for 1', () => { assert.deepEqual(getDivisors(1), [1]); - }); it('should return divisors of a number', () => { @@ -22,4 +21,4 @@ describe('Divisors', () => { assert.deepEqual(getDivisors(10), [1, 2, 5, 10]); assert.deepEqual(getDivisors(125), [1, 5, 25, 125]); }); -}); \ No newline at end of file +}); diff --git a/test/algorithms/math/fisherYates.js b/test/algorithms/math/fisherYates.js index 2d7adf68..3fb938f6 100644 --- a/test/algorithms/math/fisherYates.js +++ b/test/algorithms/math/fisherYates.js @@ -12,4 +12,4 @@ describe('Shuffle', () => { const arr = [1, 2, 3]; assert.deepEqual(Shuffle(arr).length, 3); }); -}); \ No newline at end of file +});