forked from felipernb/algorithms.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request felipernb#118 from argonlaser/master
Added findDivisors
- Loading branch information
Showing
4 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |