Open In App

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

Last Updated : 09 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
#define MAX 100001
using namespace std;
 
// Function to find all the prime
// numbers using Sieve of Eratosthenes
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 N
void 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 Code
int main()
{
    int N = 2;
    smallestSemiPrime(N);
 
    return 0;
}


Java




// Java approach for the above approach
class GFG{
 
static int MAX = 100001;
 
// Function to find all the prime
// numbers using Sieve of Eratosthenes
static 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 N
static 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 Code
public static void main(String[] args)
{
    int N = 2;
     
    smallestSemiPrime(N);
}
}
 
// This code is contributed by abhinavjain194


Python3




# Python3 program for the above approach
MAX = 100001
 
# Function to find all the prime
# numbers using Sieve of Eratosthenes
def 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 N
def 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 Code
if __name__ == "__main__":
 
    N = 2
     
    smallestSemiPrime(N)
 
# This code is contributed by ukasp


C#




// C# program for the above approach
using System;
 
class GFG{
     
static int MAX = 100001;
 
// Function to find all the prime
// numbers using Sieve of Eratosthenes
static 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 N
static 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 code
static void Main()
{
    int N = 2;
     
    smallestSemiPrime(N);
}
}
 
// This code is contributed by abhinavjain194


Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
let MAX = 100001;
  
// Function to find all the prime
// numbers using Sieve of Eratosthenes
function SieveOfEratosthenes(prime)
{
      
    // Set 0 and 1 as non-prime
    prime[0] = false;
    prime[1] = false;
  
    for(let p = 2; p * p < MAX; p++)
    {
          
        // If p is a prime
        if (prime[p] == true)
        {
              
            // Set all multiples
            // of p as non-prime
            for(let 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 N
function smallestSemiPrime(n)
{
      
    // Stores the prime numbers
    let prime = Array.from({length: MAX}, (_, i) => 0);
  
    for(let i = 0; i < prime.length; i++)
    {
        prime[i] = true;
    }
  
    // Fill the prime array
    SieveOfEratosthenes(prime);
  
    // Initialize the first divisor
    let num1 = n + 1;
  
    // Find the value of the first
    // prime number
    while (prime[num1] != true)
    {
        num1++;
    }
  
    // Initialize the second divisor
    let num2 = num1 + n;
  
    // Find the second prime number
    while (prime[num2] != true)
    {
        num2++;
    }
  
    // Print the semi-prime number
   document.write(num1 * num2);
}
 
// Driver code
 
    let N = 2;
      
    smallestSemiPrime(N);
   
  // This code is contributed by code_hunt.
</script>


Output: 

15

 

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads