Created
June 14, 2014 14:37
-
-
Save bencoveney/da085271930f3a3120e1 to your computer and use it in GitHub Desktop.
Largest prime factor
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
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