Function to return an array of prime numbers upto n using Sieve of Eratosthenes
function sieve ( n ) { let prime = [ ] ; for ( let i = 0 ; i < n + 1 ; i ++ ) prime . push ( 0 ) ; let p = 2 ; while ( p * p <= n ) { if ( prime [ p ] == 0 ) { for ( let i = 2 * p ; i < n + 1 ; i += p ) prime [ i ] = 1 ; } p += 1 ; } let allPrimes = [ ] ; for ( let i = 2 ; i < n ; i ++ ) { if ( prime [ i ] == 0 ) allPrimes . push ( i ) ; } return allPrimes ; }