Created
March 8, 2019 17:27
-
-
Save karolk/84a3ace3e240ed2dcf9ad9131d43f941 to your computer and use it in GitHub Desktop.
Finding primes brute force in finite time
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
const isPrime = number => { | |
let divisor = 2 | |
while (divisor < number) { | |
if(number % divisor === 0) return false | |
divisor += 1 | |
} | |
return true | |
} | |
const findPrime = (timeLimitMS) => { | |
let primes = [] | |
const start = Date.now() | |
const end = start + timeLimitMS | |
let number = 2 | |
while (Date.now() < end) { | |
if (isPrime(number)) { | |
primes.push(number) | |
} | |
number += 1 | |
} | |
return primes | |
} | |
// findPrime(1) | |
// > (30) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment