Created
June 9, 2022 23:50
-
-
Save blentz100/482a5b010eac1d08ccd896d8f9330030 to your computer and use it in GitHub Desktop.
Largest Prime Factor Freecodecamp Challenge
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
// https://www.freecodecamp.org/learn/coding-interview-prep/project-euler/problem-3-largest-prime-factor | |
//a prime is only divisible by 1 and itself | |
// we need a loop, with an if divisible | |
//in a loop test for all factors of given number | |
//then we need to test those for primeness | |
function largestPrimeFactor(number) { | |
for(let i = number; i >= 0; i--){ | |
//console.log('i is: ', i , "number is: ", number) | |
if(number % i == 0 && isPrime(i)){ | |
//console.log("found a factor: ", i, "checking if prime"); | |
//check if i is prime, first one should be our match | |
//console.log(isPrime(i)); | |
return i; | |
} | |
} | |
return true; | |
} | |
function isPrime(n){ | |
for(let i = 2; i < n; i++){ | |
if(n % i == 0){ | |
console.log(n, 'is not prime \n') | |
return false; | |
} | |
} | |
return true; | |
} | |
console.log("Answer: ", largestPrimeFactor(600851475143)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All test cases passing except for number = 600851475143