Smallest Semi | Java approach for the above approach ; Function to find all the prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to find the smallest semi - prime number having a difference between any of its two divisors at least N ; Stores the prime numbers ; Fill the prime array ; Initialize the first divisor ; Find the value of the first prime number ; Initialize the second divisor ; Find the second prime number ; Print the semi - prime number ; Driver Code
class GFG { static int MAX = 100001 ; static void SieveOfEratosthenes ( boolean prime [ ] ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < MAX ; i += p ) prime [ i ] = false ; } } } static void smallestSemiPrime ( int n ) { boolean [ ] prime = new boolean [ MAX ] ; for ( int i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } SieveOfEratosthenes ( prime ) ; int num1 = n + 1 ; while ( prime [ num1 ] != true ) { num1 ++ ; } int num2 = num1 + n ; while ( prime [ num2 ] != true ) { num2 ++ ; } System . out . print ( num1 * num2 ) ; } public static void main ( String [ ] args ) { int N = 2 ; smallestSemiPrime ( N ) ; } }