Skip to content

added get Divisors and Fisher Yates Algorithm #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/algorithms/math/divisors.js
Original file line number Diff line number Diff line change
@@ -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;
22 changes: 22 additions & 0 deletions src/algorithms/math/fisher_Yates.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions src/algorithms/math/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions test/algorithms/math/divisors.js
Original file line number Diff line number Diff line change
@@ -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]);
});
});
15 changes: 15 additions & 0 deletions test/algorithms/math/fisherYates.js
Original file line number Diff line number Diff line change
@@ -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);
});
});