Skip to content

Instantly share code, notes, and snippets.

@jiggzson
Last active August 29, 2015 14:09
Show Gist options
  • Save jiggzson/8eab455503bdd1f5a423 to your computer and use it in GitHub Desktop.
Save jiggzson/8eab455503bdd1f5a423 to your computer and use it in GitHub Desktop.
Get all the factors of an integer
function factors(num, unique) {
var l = num, i=1, factors = [],
epsilon = 2.2204460492503130808472633361816E-16;
while(i<l) {
var quotient = num/i;
var whole = Math.floor(quotient);
var remainder = quotient-whole;
if(remainder <= epsilon) {
unique && i === whole ? factors.push(i) : factors.push(i, whole);
l = whole;
}
i++;
}
return factors.sort();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment