Find all the prime numbers of given number of digits | Javascript implementation of the approach ; Function for Sieve of Eratosthenes ; Function to print all the prime numbers with d digits ; Range to check integers ; For every integer in the range ; If the current integer is prime ; Generate primes
let sz = 100000 ; let isPrime = new Array ( sz + 1 ) ; isPrime . fill ( false ) ; function sieve ( ) { for ( let i = 0 ; i <= sz ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( let i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( let j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } function findPrimesD ( d ) { let left = Math . pow ( 10 , d - 1 ) ; let right = Math . pow ( 10 , d ) - 1 ; for ( let i = left ; i <= right ; i ++ ) { if ( isPrime [ i ] ) { document . write ( i + " " ) ; } } } sieve ( ) ; let d = 1 ; findPrimesD ( d ) ;