Smallest Semi | C ++ program 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
#include <bits/stdc++.h> NEW_LINE #define MAX  100001 NEW_LINE using namespace std ; void SieveOfEratosthenes ( bool 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 ; } } } void smallestSemiPrime ( int n ) { bool prime [ MAX ] ; memset ( prime , true , sizeof ( prime ) ) ; SieveOfEratosthenes ( prime ) ; int num1 = n + 1 ; while ( prime [ num1 ] != true ) { num1 ++ ; } int num2 = num1 + n ; while ( prime [ num2 ] != true ) { num2 ++ ; } cout << num1 * 1LL * num2 ; } int main ( ) { int N = 2 ; smallestSemiPrime ( N ) ; return 0 ; }