Segmented Sieve | This functions finds all primes smaller than ' limit ' using simple sieve of eratosthenes . ; Create a boolean array " mark [ 0 . . limit - 1 ] " and initialize all entries of it as true . A value in mark [ p ] will finally be false if ' p ' is Not a prime , else true . ; One by one traverse all numbers so that their multiples can be marked as composite . ; If p is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers and store them in prime
function simpleSieve ( limit ) { var mark = Array ( limit ) . fill ( true ) ; for ( p = 2 ; p * p < limit ; p ++ ) { if ( mark [ p ] == true ) { for ( i = p * p ; i < limit ; i += p ) mark [ i ] = false ; } } for ( p = 2 ; p < limit ; p ++ ) if ( mark [ p ] == true ) document . write ( p + " " ) ; }