📌  相关文章
📜  具有至少 K 个素因数且每个因子之间的差至少为 D 的最小整数

📅  最后修改于: 2022-05-13 01:56:09.279000             🧑  作者: Mango

具有至少 K 个素因数且每个因子之间的差至少为 D 的最小整数

给定两个整数DK 。任务是找到具有至少K个素因数且每对因数之间的差至少为D的最小数N。

例子

方法:这个问题可以通过使用埃拉托色尼筛来解决 按照以下步骤解决给定的问题。

  • 做一个埃拉托色尼筛子。
  • 初始化变量firstDivisor并存储D + 1
  • 将 firstDivisor迭代1直到它变为素数。
  • 初始化SmallestNumber = FirstDivisor + D
  • 现在迭代循环并增加 SmallestNumber 直到我们得到K -1个素数。
  • 并且,产品与所有除数并返回产品。

下面是上述方法的实现。

C++
// C++ program for above approach
#include 
using namespace std;
const int N = 1000000;
 
// Function of Sieve of Eratosthenes
void SieveOfEratosthenes(vector& 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 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 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


输出
210

时间复杂度: O(N)
辅助空间: O(N)