Smallest composite number not divisible by first N prime numbers | Java program to implement the above approach ; Initializing the max value ; Function to generate N prime numbers using Sieve of Eratosthenes ; Stores the primes ; Setting all numbers to be prime initially ; If a prime number is encountered ; Set all its multiples as composites ; Store all the prime numbers ; Function to find the square of the ( N + 1 ) - th prime number ; Driver Code ; Stores all prime numbers
import java . util . Arrays ; import java . util . Vector ; class GFG { static final int MAX_SIZE = 1000005 ; static void SieveOfEratosthenes ( Vector < Integer > StorePrimes ) { boolean [ ] IsPrime = new boolean [ MAX_SIZE ] ; Arrays . fill ( IsPrime , true ) ; for ( int p = 2 ; p * p < MAX_SIZE ; p ++ ) { if ( IsPrime [ p ] == true ) { for ( int i = p * p ; i < MAX_SIZE ; i += p ) IsPrime [ i ] = false ; } } for ( int p = 2 ; p < MAX_SIZE ; p ++ ) if ( IsPrime [ p ] ) StorePrimes . add ( p ) ; } static int Smallest_non_Prime ( Vector < Integer > StorePrimes , int N ) { int x = StorePrimes . get ( N ) ; return x * x ; } public static void main ( String [ ] args ) { int N = 3 ; Vector < Integer > StorePrimes = new Vector < Integer > ( ) ; SieveOfEratosthenes ( StorePrimes ) ; System . out . print ( Smallest_non_Prime ( StorePrimes , N ) ) ; } }