Skip to content

Added findDivisors #118

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

Merged
merged 1 commit into from
Sep 8, 2016
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ require('algorithms').Math;
* extendedEuclidean
* fastPower
* fibonacci
* findDivisors
* fisherYates
* gcd (Greatest common divisor)
* greatestDifference
Expand Down
91 changes: 91 additions & 0 deletions src/algorithms/math/find_divisors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict';

/**
* Different implementations for finding the divisors
*/

/**
* Find all the divisors of a natural number
* This solution has a cost of O(n)
* This solution uses the naive method to print all divisors
*
* @param {number}
* @returns {number[]} - returns the divisors
*/

var findDivisorsGeneric = function(number) {
var index = 1;
var divisors = [];

for (index; index <= number; index++) {
if (number % index === 0) {
divisors.push(index);
}
}
return divisors;
};

/**
* Find all the divisors of a natural number
* This solution has a cost of O(sqrt(n))
* This method returns the divisors in an unsorted manner.
* All divisors of a number are present in pairs.
* Eg : For n=16: (1, 16), (2, 8), (4, 4). Include only one of the repeated divisors if any.
*
* @param {number}
* @returns {number[]} - returns the divisors
*/

var findDivisorsByPairingUnsorted = function(number) {
var index = 1;
var divisors = [];

for (index; index <= Math.sqrt(number); index++) {
if (number % index === 0) {
if (number / index === index)
divisors.push(index);
else {
divisors.push(index);
divisors.push(number / index);
}
}
}
return divisors;
};

/**
* Find all the divisors of a natural number
* This solution has a cost of O(sqrt(n))
* This method returns the array in a sorted manner
* All divisors of a number are present in pairs (divisorLessThanSqrt, divisorGreaterThanSqrt)
* Reverse the divisorGreaterThanSqrt array and append to divisorLessThanSqrt to sort the result
* Eg : For n=16: (1, 16), (2, 8), (4, 4). Include only one of the repeated divisors if any
*
* @param {number}
* @returns {number[]} - returns the divisors
*/

var findDivisorsByPairingSorted = function(number) {
var index = 1;
var divisors = [];
var divisorsLessThanSqrt = [];
var divisorsMoreThanSqrt = [];

for (index; index <= Math.sqrt(number); index++) {
if (number % index === 0) {
if (number / index === index)
divisorsLessThanSqrt.push(index);
else {
divisorsLessThanSqrt.push(index);
divisorsMoreThanSqrt.push(number / index);
}
}
}
divisors = divisorsLessThanSqrt.concat(divisorsMoreThanSqrt.reverse());
return divisors;
};

// Use findDivisorsGeneric as the default implementation
findDivisorsGeneric.pairingUnsorted = findDivisorsByPairingUnsorted;
findDivisorsGeneric.pairingSorted = findDivisorsByPairingSorted;
module.exports = findDivisorsGeneric;
3 changes: 2 additions & 1 deletion src/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ module.exports = {
powerSet: require('./algorithms/math/power_set'),
shannonEntropy: require('./algorithms/math/shannon_entropy'),
collatzConjecture: require('./algorithms/math/collatz_conjecture'),
greatestDifference: require('./algorithms/math/greatest_difference')
greatestDifference: require('./algorithms/math/greatest_difference'),
findDivisors: require('./algorithms/math/find_divisors')
};
52 changes: 52 additions & 0 deletions src/test/algorithms/math/find_divisors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

var math = require('../../..').Math;
var findDivisors = math.findDivisors;
var assert = require('assert');

/**
* Deep equal for arrays
*/
function testArrayEqual(a, b) {
var arrayEqual = true;
a.sort();
b.sort();
a.forEach(function(elem, index) {
if (a[index] !== b[index]) {
arrayEqual = false;
}
});
return arrayEqual && a.length === b.length;
}

var testFindDivisors = function(findDivisors) {
assert(testArrayEqual([], findDivisors(-2)));
assert(testArrayEqual([], findDivisors(0)));
assert(testArrayEqual([1], findDivisors(1)));
assert(testArrayEqual([1, 2], findDivisors(2)));
assert(testArrayEqual([1, 3], findDivisors(3)));
assert(testArrayEqual([1, 2, 4], findDivisors(4)));
assert(testArrayEqual([1, 5], findDivisors(5)));
assert(testArrayEqual([1, 2, 4, 5, 10, 20, 25, 50, 100], findDivisors(100)));
assert(testArrayEqual([1, 2, 7, 13, 14, 26, 91, 182], findDivisors(182)));
};

describe('Find divisors', function() {
describe('#Generic()', function() {
it('should return the divisors of the number', function() {
testFindDivisors(findDivisors);
});
});

describe('#PairingUnsorted()', function() {
it('should return the divisors of the number', function() {
testFindDivisors(findDivisors.pairingUnsorted);
});
});

describe('#PairingSorted()', function() {
it('should return the divisors of the number', function() {
testFindDivisors(findDivisors.pairingSorted);
});
});
});