Monday, October 7, 2024
Google search engine
HomeData Modelling & AISmallest Semi-Prime Number with at least N difference between any two of...

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

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)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
09 Jun, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments