Function to precompute all the primes upto 1000000 and store it in a set using Sieve of Eratosthenes
function getPrimesFromSeive ( primes ) { let prime = new Array ( 1000001 ) ; for ( let i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( let i = 2 ; i * i <= 1000000 ; i ++ ) { if ( prime [ i ] == true ) { for ( let j = i * i ; j <= 1000000 ; j += i ) prime [ j ] = false ; } }