diff --git a/src/algorithms/math/divisors.js b/src/algorithms/math/divisors.js new file mode 100644 index 00000000..e41d23bc --- /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..d7239d4f --- /dev/null +++ b/src/algorithms/math/fisher_Yates.js @@ -0,0 +1,22 @@ +/** + * 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; 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..f008c0db --- /dev/null +++ b/test/algorithms/math/divisors.js @@ -0,0 +1,24 @@ +/* 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]); + }); +}); diff --git a/test/algorithms/math/fisherYates.js b/test/algorithms/math/fisherYates.js new file mode 100644 index 00000000..3fb938f6 --- /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); + }); +});