Created
July 15, 2017 22:15
-
-
Save suissa/974621fd8e70313642534bb0b806a8ef to your computer and use it in GitHub Desktop.
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
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
// The prime factors of 13195 are 5, 7, 13 and 29. | |
// What is the largest prime factor of the number 600851475143 ? | |
const NUM = 600851475143 | |
const getLength = ( n ) => Math.floor( Math.sqrt( n ) ) - 1 | |
const isPrime = ( n ) => | |
( n > 1 && Array.from( { length: getLength( n ) }, ( v, k ) => k + 2 ) | |
.every( m => n % m ) | |
) | |
const generateRange = ( from, to ) => | |
Array.from( { length: to - from + 1 }, ( v, k ) => k + from) | |
const from = 1 | |
const to = Math.sqrt( NUM ) | |
console.log ( | |
`largest prime factor of the number 600851475143 are : `, | |
generateRange( from, to ).filter( isPrime ) | |
.filter( ( prime ) => NUM % prime === 0 ) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment