Skip to content

Commit d71ac43

Browse files
authored
Merge pull request felipernb#118 from argonlaser/master
Added findDivisors
2 parents dec8937 + 3f4e7e0 commit d71ac43

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ require('algorithms').Math;
8888
* extendedEuclidean
8989
* fastPower
9090
* fibonacci
91+
* findDivisors
9192
* fisherYates
9293
* gcd (Greatest common divisor)
9394
* greatestDifference

src/algorithms/math/find_divisors.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
2+
3+
/**
4+
* Different implementations for finding the divisors
5+
*/
6+
7+
/**
8+
* Find all the divisors of a natural number
9+
* This solution has a cost of O(n)
10+
* This solution uses the naive method to print all divisors
11+
*
12+
* @param {number}
13+
* @returns {number[]} - returns the divisors
14+
*/
15+
16+
var findDivisorsGeneric = function(number) {
17+
var index = 1;
18+
var divisors = [];
19+
20+
for (index; index <= number; index++) {
21+
if (number % index === 0) {
22+
divisors.push(index);
23+
}
24+
}
25+
return divisors;
26+
};
27+
28+
/**
29+
* Find all the divisors of a natural number
30+
* This solution has a cost of O(sqrt(n))
31+
* This method returns the divisors in an unsorted manner.
32+
* All divisors of a number are present in pairs.
33+
* Eg : For n=16: (1, 16), (2, 8), (4, 4). Include only one of the repeated divisors if any.
34+
*
35+
* @param {number}
36+
* @returns {number[]} - returns the divisors
37+
*/
38+
39+
var findDivisorsByPairingUnsorted = function(number) {
40+
var index = 1;
41+
var divisors = [];
42+
43+
for (index; index <= Math.sqrt(number); index++) {
44+
if (number % index === 0) {
45+
if (number / index === index)
46+
divisors.push(index);
47+
else {
48+
divisors.push(index);
49+
divisors.push(number / index);
50+
}
51+
}
52+
}
53+
return divisors;
54+
};
55+
56+
/**
57+
* Find all the divisors of a natural number
58+
* This solution has a cost of O(sqrt(n))
59+
* This method returns the array in a sorted manner
60+
* All divisors of a number are present in pairs (divisorLessThanSqrt, divisorGreaterThanSqrt)
61+
* Reverse the divisorGreaterThanSqrt array and append to divisorLessThanSqrt to sort the result
62+
* Eg : For n=16: (1, 16), (2, 8), (4, 4). Include only one of the repeated divisors if any
63+
*
64+
* @param {number}
65+
* @returns {number[]} - returns the divisors
66+
*/
67+
68+
var findDivisorsByPairingSorted = function(number) {
69+
var index = 1;
70+
var divisors = [];
71+
var divisorsLessThanSqrt = [];
72+
var divisorsMoreThanSqrt = [];
73+
74+
for (index; index <= Math.sqrt(number); index++) {
75+
if (number % index === 0) {
76+
if (number / index === index)
77+
divisorsLessThanSqrt.push(index);
78+
else {
79+
divisorsLessThanSqrt.push(index);
80+
divisorsMoreThanSqrt.push(number / index);
81+
}
82+
}
83+
}
84+
divisors = divisorsLessThanSqrt.concat(divisorsMoreThanSqrt.reverse());
85+
return divisors;
86+
};
87+
88+
// Use findDivisorsGeneric as the default implementation
89+
findDivisorsGeneric.pairingUnsorted = findDivisorsByPairingUnsorted;
90+
findDivisorsGeneric.pairingSorted = findDivisorsByPairingSorted;
91+
module.exports = findDivisorsGeneric;

src/math.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ module.exports = {
1515
powerSet: require('./algorithms/math/power_set'),
1616
shannonEntropy: require('./algorithms/math/shannon_entropy'),
1717
collatzConjecture: require('./algorithms/math/collatz_conjecture'),
18-
greatestDifference: require('./algorithms/math/greatest_difference')
18+
greatestDifference: require('./algorithms/math/greatest_difference'),
19+
findDivisors: require('./algorithms/math/find_divisors')
1920
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
var math = require('../../..').Math;
4+
var findDivisors = math.findDivisors;
5+
var assert = require('assert');
6+
7+
/**
8+
* Deep equal for arrays
9+
*/
10+
function testArrayEqual(a, b) {
11+
var arrayEqual = true;
12+
a.sort();
13+
b.sort();
14+
a.forEach(function(elem, index) {
15+
if (a[index] !== b[index]) {
16+
arrayEqual = false;
17+
}
18+
});
19+
return arrayEqual && a.length === b.length;
20+
}
21+
22+
var testFindDivisors = function(findDivisors) {
23+
assert(testArrayEqual([], findDivisors(-2)));
24+
assert(testArrayEqual([], findDivisors(0)));
25+
assert(testArrayEqual([1], findDivisors(1)));
26+
assert(testArrayEqual([1, 2], findDivisors(2)));
27+
assert(testArrayEqual([1, 3], findDivisors(3)));
28+
assert(testArrayEqual([1, 2, 4], findDivisors(4)));
29+
assert(testArrayEqual([1, 5], findDivisors(5)));
30+
assert(testArrayEqual([1, 2, 4, 5, 10, 20, 25, 50, 100], findDivisors(100)));
31+
assert(testArrayEqual([1, 2, 7, 13, 14, 26, 91, 182], findDivisors(182)));
32+
};
33+
34+
describe('Find divisors', function() {
35+
describe('#Generic()', function() {
36+
it('should return the divisors of the number', function() {
37+
testFindDivisors(findDivisors);
38+
});
39+
});
40+
41+
describe('#PairingUnsorted()', function() {
42+
it('should return the divisors of the number', function() {
43+
testFindDivisors(findDivisors.pairingUnsorted);
44+
});
45+
});
46+
47+
describe('#PairingSorted()', function() {
48+
it('should return the divisors of the number', function() {
49+
testFindDivisors(findDivisors.pairingSorted);
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)