Skip to content

Instantly share code, notes, and snippets.

@Trott
Created January 7, 2019 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Trott/a85f99c5c5e18299f13a61616934b9d7 to your computer and use it in GitHub Desktop.
Save Trott/a85f99c5c5e18299f13a61616934b9d7 to your computer and use it in GitHub Desktop.
'use strict';
const min = 2;
const max = 1e7;
const primes = [];
function generatePrimes(start, range) {
let isPrime = true;
let end = start + range;
for (let i = start; i < end; i++) {
for (let j = min; j < Math.sqrt(end); j++) {
if (i !== j && i%j === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(i);
}
isPrime = true;
}
}
generatePrimes(min, max);
console.log(primes.join('\n'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment