Find all the prime numbers of given number of digits | Java 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 ; Driver code ; Generate primes
import java . util . * ; class GFG { static int sz = 100000 ; static boolean isPrime [ ] = new boolean [ sz + 1 ] ; static void sieve ( ) { for ( int i = 0 ; i <= sz ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } static void findPrimesD ( int d ) { int left = ( int ) Math . pow ( 10 , d - 1 ) ; int right = ( int ) Math . pow ( 10 , d ) - 1 ; for ( int i = left ; i <= right ; i ++ ) { if ( isPrime [ i ] ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String args [ ] ) { sieve ( ) ; int d = 1 ; findPrimesD ( d ) ; } }