Use Sieve of Eratosthenes to find all primes smaller than n
let isPrime = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) isPrime [ i ] = true ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( isPrime [ i ] ) { for ( let j = 2 * i ; j <= n ; j += i ) { isPrime [ j ] = false ; } } }