# Smallest Semi-Prime Number with at least N difference between any two of its divisors

• Last Updated : 09 Jun, 2021

Given a positive integer N, the task is to find the smallest semi-prime number such that the difference between any of its two divisors is at least N.

Examples:

Input: N = 2
Output: 15
Explanation:
The divisors of 15 are 1, 3, 5, and 15 and the difference between any of its two divisors is greater than or equal to N(= 2).

Input: N = 3
Output: 55

Approach: The given problem can be solved by finding two prime numbers(say X and Y) whose difference is at least N. The idea is to find the first prime number i.e., X greater than N, and the second prime number i.e., Y greater than (N + X). Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include #define MAX 100001using namespace std; // Function to find all the prime// numbers using Sieve of Eratosthenesvoid SieveOfEratosthenes(bool prime[]){    // Set 0 and 1 as non-prime    prime[0] = false;    prime[1] = false;     for (int p = 2; p * p < MAX; p++) {         // If p is a prime        if (prime[p] == true) {             // Set all multiples            // of p as non-prime            for (int i = p * p; i < MAX; i += p)                prime[i] = false;        }    }} // Function to find the smallest semi-prime// number having a difference between any// of its two divisors at least Nvoid smallestSemiPrime(int n){    // Stores the prime numbers    bool prime[MAX];    memset(prime, true, sizeof(prime));     // Fill the prime array    SieveOfEratosthenes(prime);     // Initialize the first divisor    int num1 = n + 1;     // Find the value of the first    // prime number    while (prime[num1] != true) {        num1++;    }     // Initialize the second divisor    int num2 = num1 + n;     // Find the second prime number    while (prime[num2] != true) {        num2++;    }     // Print the semi-prime number    cout << num1 * 1LL * num2;} // Driver Codeint main(){    int N = 2;    smallestSemiPrime(N);     return 0;}

## Java

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

## Python3

 # Python3 program for the above approachMAX = 100001 # Function to find all the prime# numbers using Sieve of Eratosthenesdef SieveOfEratosthenes(prime):     # Set 0 and 1 as non-prime    prime[0] = False    prime[1] = False     p = 2         while p * p < MAX:         # If p is a prime        if (prime[p] == True):             # Set all multiples            # of p as non-prime            for i in range(p * p, MAX, p):                prime[i] = False         p += 1 # Function to find the smallest semi-prime# number having a difference between any# of its two divisors at least Ndef smallestSemiPrime(n):     # Stores the prime numbers    prime = [True] * MAX     # Fill the prime array    SieveOfEratosthenes(prime)     # Initialize the first divisor    num1 = n + 1     # Find the value of the first    # prime number    while (prime[num1] != True):        num1 += 1     # Initialize the second divisor    num2 = num1 + n     # Find the second prime number    while (prime[num2] != True):        num2 += 1     # Print the semi-prime number    print(num1 * num2) # Driver Codeif __name__ == "__main__":     N = 2         smallestSemiPrime(N) # This code is contributed by ukasp

## C#

 // C# program for the above approachusing System; class GFG{     static int MAX = 100001; // Function to find all the prime// numbers using Sieve of Eratosthenesstatic void SieveOfEratosthenes(bool[] prime){         // Set 0 and 1 as non-prime    prime[0] = false;    prime[1] = false;     for(int p = 2; p * p < MAX; p++)    {                 // If p is a prime        if (prime[p] == true)        {                         // Set all multiples            // of p as non-prime            for(int i = p * p; i < MAX; i += p)                prime[i] = false;        }    }} // Function to find the smallest semi-prime// number having a difference between any// of its two divisors at least Nstatic void smallestSemiPrime(int n){         // Stores the prime numbers    bool[] prime = new bool[MAX];         for(int i = 0; i < prime.Length; i++)    {        prime[i] = true;    }         // Fill the prime array    SieveOfEratosthenes(prime);     // Initialize the first divisor    int num1 = n + 1;     // Find the value of the first    // prime number    while (prime[num1] != true)    {        num1++;    }     // Initialize the second divisor    int num2 = num1 + n;     // Find the second prime number    while (prime[num2] != true)    {        num2++;    }     // Print the semi-prime number    Console.Write(num1 * num2);} // Driver codestatic void Main(){    int N = 2;         smallestSemiPrime(N);}} // This code is contributed by abhinavjain194

## Javascript



Output:

15

Time Complexity: O(M * log(log(M))), where M is the size of the prime[] array
Auxiliary Space: O(M)

My Personal Notes arrow_drop_up