Skip to content
Related Articles

Related Articles

Smallest integer having at least K prime divisors with difference between each factor at least D

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 31 Mar, 2022

Given two integers D and K. The task is to find the smallest number N which has at least K prime divisors and the difference between each pair of divisors is at least D

Examples

Input: D = 3, K = 2
Output: 55
Explanation: It is smallest number which has 4 divisors 1 and 2 prime divisors  5, 11 and their difference between any of the pair is D.

Input: D = 1, K = 4
Output: 210
Explanation: It is the smallest number which has 5 divisors 1 and 4 prime divisors 2, 3, 5, 7, and their difference between any of the pair is D.

 

Approach: This problem can be solved by using the Sieve of Eratosthenes Follow the steps below to solve the given problem.

  • Make a sieve of Eratosthenes.
  • Initialize a variable firstDivisor and store D + 1.
  • Iterate firstDivisor by 1 until it becomes prime.
  • Initialize SmallestNumber = FirstDivisor + D.
  • Now iterate the loop and increment SmallestNumber until we get K -1 primes.
  • And, the product with all divisors and return the product.

Below is the implementation of the above approach.

C++




// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
 
// Function of Sieve of Eratosthenes
void SieveOfEratosthenes(vector<bool>& prime)
{
    for (int p = 2; p * p <= N; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i <= N; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to find smallest
// number with given conditions
int SmallestNumber(vector<bool> prime,
                   int D, int K)
{
 
    // Initialize first with D + 1
    // because 1 is also a divisor
    int FirstDivisor = D + 1;
 
    while (FirstDivisor < N
           and !prime[FirstDivisor]) {
        ++FirstDivisor;
    }
 
    // Now value of K is decrement by 1
    K--;
 
    // Initialize Divisor with First + D
    // to maintain a difference D
    // We get Remaining divisor
    int SmallestNumber = FirstDivisor;
    int Divisor = FirstDivisor + D;
 
    // Maintain previous divisor
    // to maintain difference
    int prevDivisor = FirstDivisor;
    while (K > 0 and SmallestNumber < N) {
        if (prime[Divisor]
            and Divisor - D >= prevDivisor) {
            SmallestNumber *= Divisor;
            prevDivisor = Divisor;
            K--;
        }
        Divisor++;
    }
 
    // Return the final answer
    return SmallestNumber;
}
 
// Driver Code
int main()
{
    vector<bool> prime(N, true);
 
    SieveOfEratosthenes(prime);
 
    int D = 1;
    int K = 4;
 
    // Function Call
    cout << SmallestNumber(prime, D, K);
 
    return 0;
}

Java




// Java program to implement
// the above approach
import java.util.*;
 
class GFG
{
 
static int N = 1000000;
 
// Function of Sieve of Eratosthenes
static void SieveOfEratosthenes(boolean[] prime)
{
    for (int p = 2; p * p < N; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i < N; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to find smallest
// number with given conditions
static int SmallestNumber(boolean[] prime,
                   int D, int K)
{
 
    // Initialize first with D + 1
    // because 1 is also a divisor
    int FirstDivisor = D + 1;
 
    while (FirstDivisor < N
           && !prime[FirstDivisor]) {
        ++FirstDivisor;
    }
 
    // Now value of K is decrement by 1
    K--;
 
    // Initialize Divisor with First + D
    // to maintain a difference D
    // We get Remaining divisor
    int SmallestNumber = FirstDivisor;
    int Divisor = FirstDivisor + D;
 
    // Maintain previous divisor
    // to maintain difference
    int prevDivisor = FirstDivisor;
    while (K > 0 && SmallestNumber < N) {
        if (prime[Divisor]
            && Divisor - D >= prevDivisor) {
            SmallestNumber *= Divisor;
            prevDivisor = Divisor;
            K--;
        }
        Divisor++;
    }
 
    // Return the final answer
    return SmallestNumber;
}
 
// Driver Code
public static void main(String args[])
{
    boolean[] prime = new boolean[N];
    Arrays.fill(prime, true);
 
    SieveOfEratosthenes(prime);
 
    int D = 1;
    int K = 4;
 
    // Function Call
    System.out.println(SmallestNumber(prime, D, K));
}
}
 
// This code is contributed by sanjoy_62.

Python3




# Python program to implement
# the above approach
N = 1000000;
 
# Function of Sieve of Eratosthenes
def SieveOfEratosthenes(prime):
    for p in range(2, N//2):
        if (prime[p] == True):
            for i in range(p*p,N,p):
                prime[i] = False;
         
# Function to find smallest
# number with given conditions
def SmallestNumber(prime, D, K):
 
    # Initialize first with D + 1
    # because 1 is also a divisor
    FirstDivisor = D + 1;
 
    while (FirstDivisor < N and prime[FirstDivisor]!=True):
        FirstDivisor += 1;
     
    # Now value of K is decrement by 1
    K -= 1;
 
    # Initialize Divisor with First + D
    # to maintain a difference D
    # We get Remaining divisor
    SmallestNumber = FirstDivisor;
    Divisor = FirstDivisor + D;
 
    # Maintain previous divisor
    # to maintain difference
    prevDivisor = FirstDivisor;
    while (K > 0 and SmallestNumber < N):
        if (prime[Divisor] and Divisor - D >= prevDivisor):
            SmallestNumber *= Divisor;
            prevDivisor = Divisor;
            K -= 1;
         
        Divisor += 1;
 
    # Return the final answer
    return SmallestNumber;
 
# Driver Code
if __name__ == '__main__':
    prime = [True for i in range(N)];
     
    SieveOfEratosthenes(prime);
 
    D = 1;
    K = 4;
 
    # Function Call
    print(SmallestNumber(prime, D, K));
 
# This code is contributed by Rajput-Ji

C#




// C# program to implement
// the above approach
using System;
 
public class GFG
{
 
  static int N = 1000000;
 
  // Function of Sieve of Eratosthenes
  static void SieveOfEratosthenes(bool[] prime)
  {
    for (int p = 2; p * p < N; p++) {
      if (prime[p] == true) {
        for (int i = p * p; i < N; i += p)
          prime[i] = false;
      }
    }
  }
 
  // Function to find smallest
  // number with given conditions
  static int SmallestNumber(bool[] prime,
                            int D, int K)
  {
 
    // Initialize first with D + 1
    // because 1 is also a divisor
    int FirstDivisor = D + 1;
 
    while (FirstDivisor < N
           && !prime[FirstDivisor]) {
      ++FirstDivisor;
    }
 
    // Now value of K is decrement by 1
    K--;
 
    // Initialize Divisor with First + D
    // to maintain a difference D
    // We get Remaining divisor
    int SmallestNumber = FirstDivisor;
    int Divisor = FirstDivisor + D;
 
    // Maintain previous divisor
    // to maintain difference
    int prevDivisor = FirstDivisor;
    while (K > 0 && SmallestNumber < N) {
      if (prime[Divisor]
          && Divisor - D >= prevDivisor) {
        SmallestNumber *= Divisor;
        prevDivisor = Divisor;
        K--;
      }
      Divisor++;
    }
 
    // Return the readonly answer
    return SmallestNumber;
  }
 
  // Driver Code
  public static void Main(String []args)
  {
    bool[] prime = new bool[N];
    for(int i = 0;i<N;i++)
      prime[i] = true;
 
    SieveOfEratosthenes(prime);
 
    int D = 1;
    int K = 4;
 
    // Function Call
    Console.WriteLine(SmallestNumber(prime, D, K));
  }
}
 
// This code is contributed by Rajput-Ji

Javascript




<script>
        // JavaScript code for the above approach
 
        let N = 1000000;
 
        // Function of Sieve of Eratosthenes
        function SieveOfEratosthenes(prime) {
            for (let p = 2; p * p <= N; p++) {
                if (prime[p] == true) {
                    for (let i = p * p; i <= N; i += p)
                        prime[i] = false;
                }
            }
        }
 
        // Function to find smallest
        // number with given conditions
        function SmallestNumber(prime,
            D, K) {
 
            // Initialize first with D + 1
            // because 1 is also a divisor
            let FirstDivisor = D + 1;
 
            while (FirstDivisor < N
                && !prime[FirstDivisor]) {
                ++FirstDivisor;
            }
 
            // Now value of K is decrement by 1
            K--;
 
            // Initialize Divisor with First + D
            // to maintain a difference D
            // We get Remaining divisor
            let SmallestNumber = FirstDivisor;
            let Divisor = FirstDivisor + D;
 
            // Maintain previous divisor
            // to maintain difference
            let prevDivisor = FirstDivisor;
            while (K > 0 && SmallestNumber < N) {
                if (prime[Divisor]
                    && Divisor - D >= prevDivisor) {
                    SmallestNumber *= Divisor;
                    prevDivisor = Divisor;
                    K--;
                }
                Divisor++;
            }
 
            // Return the final answer
            return SmallestNumber;
        }
 
        // Driver Code
        let prime = new Array(N).fill(true);
 
        SieveOfEratosthenes(prime);
 
        let D = 1;
        let K = 4;
 
        // Function Call
        document.write(SmallestNumber(prime, D, K))
 
       // This code is contributed by Potta Lokesh
    </script>

Output

210

Time Complexity: O(N) 
Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!