Smallest Semi | JavaScript program to implement 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
let MAX = 100001 ; function SieveOfEratosthenes ( prime ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i < MAX ; i += p ) prime [ i ] = false ; } } } function smallestSemiPrime ( n ) { let prime = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } SieveOfEratosthenes ( prime ) ; let num1 = n + 1 ; while ( prime [ num1 ] != true ) { num1 ++ ; } let num2 = num1 + n ; while ( prime [ num2 ] != true ) { num2 ++ ; } document . write ( num1 * num2 ) ; } let N = 2 ; smallestSemiPrime ( N ) ;