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

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 ``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

## Javascript

 ``

Output
`210`

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

Previous
Next