Skip to content

Instantly share code, notes, and snippets.

@karolk
Created March 8, 2019 17:27
Show Gist options
  • Save karolk/84a3ace3e240ed2dcf9ad9131d43f941 to your computer and use it in GitHub Desktop.
Save karolk/84a3ace3e240ed2dcf9ad9131d43f941 to your computer and use it in GitHub Desktop.
Finding primes brute force in finite time
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