-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d06616e
commit 6cd0907
Showing
1 changed file
with
42 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
let primeFactors = [] | ||
|
||
let generatePrimeFactors = (number) => { | ||
|
||
// Qutiient keeps track of result after division | ||
let quotient = number | ||
|
||
/* If Even, keep dividing by two */ | ||
while(quotient % 2 === 0){ | ||
primeFactors.push(2) | ||
quotient = quotient / 2 | ||
} | ||
|
||
/* Quotient is odd at this point, so now find odd prime factors */ | ||
let i | ||
// Look through odd numbers between 3 and quotient | ||
for(i = 3; i < quotient; i = i + 2){ | ||
// If we can quotient by the odd number without a remainder, divide it | ||
while(quotient % i === 0){ | ||
primeFactors.push(i) | ||
quotient = quotient / i | ||
} | ||
} | ||
// If the remaining quotient is a prime number, push to array | ||
if(quotient !== 1){ | ||
primeFactors.push(quotient) | ||
} | ||
|
||
} | ||
|
||
/* Solution Function */ | ||
let largestPrimeFactor = (number) => { | ||
|
||
generatePrimeFactors(number) | ||
|
||
// Converts array into series of function arguments fed into Math.max | ||
return Math.max(...primeFactors) | ||
} | ||
|
||
|
||
/* Check Solution */ | ||
console.log(largestPrimeFactor(600851475143)) |