Skip to content

Instantly share code, notes, and snippets.

@bencoveney
Created June 14, 2014 14:37
Show Gist options
  • Save bencoveney/da085271930f3a3120e1 to your computer and use it in GitHub Desktop.
Save bencoveney/da085271930f3a3120e1 to your computer and use it in GitHub Desktop.
Largest prime factor
var number = 600851475143;
var findFactor = function(number)
{
console.log("Finding factors of " + number);
// Loop through all possible factors beginning from 2
// 2 is chosen because 1 will always be a factor
for(var i = 2; i < number / 2; i++)
{
// If the number is a factor then divide by it and repeat the process
if(number % i === 0)
{
console.log(i + " found as a factor")
return findFactor(number / i);
}
}
// If no factors were found then this factor is prime
console.log("No factors found")
return number;
}
console.log("Largest factor of " + number + " is " + findFactor(number));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment