📜  最小的数只有 4 个除数,最多有两个差 D

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

最小的数只有 4 个除数,最多有两个差 D

给定数字D ,找到最小的数字N ,使其恰好有四个除数,并且其中任何两个除数之间的差大于或等于D

例子:

方法:很明显 '1' 和数字本身是N的除数。所以恰好有 4 个除数的数的除数分别为1、a、b、a*b 。对于它恰好有 4 个除数的条件, ab都必须是素数。对于任意两者之差至少为D的条件,从1+d开始寻找a并检查它是否为素数,如果不是素数,我们将找到它旁边的素数。同样,从a + d开始查找b并检查它是否为素数,并执行与查找a相同的操作。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
int next_prime(int x)
{
    // Edge case
    if (x == 1 || x == 2) {
        return 2;
    }
 
    // Checking if x is prime
    bool is_prime = false;
 
    // Loop until next prime is found
    while (!is_prime) {
        is_prime = true;
        for (int i = 2; i <= sqrt(x); i++) {
            if (x % i == 0) {
                is_prime = false;
            }
        }
        x++;
    }
    return x - 1;
}
 
// Function to find the number
int findN(int D)
{
    // Assuming 1+D as first required
    // divisor because it is
    // at distance D from 1
    int a = 1 + D;
 
    // Checking whether 1+D is prime or not
    // otherwise it will return prime number
    // just next to it
    a = next_prime(a);
 
    // Checking the same for next divisor
    int b = a + D;
    b = next_prime(b);
 
    int N = a * b;
    return N;
}
 
// Driver Code
int main()
{
    int D = 2;
 
    int ans = findN(D);
    cout << ans;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  static int next_prime(int x)
  {
    // Edge case
    if (x == 1 || x == 2) {
      return 2;
    }
 
    // Checking if x is prime
    Boolean is_prime = false;
 
    // Loop until next prime is found
    while (!is_prime) {
      is_prime = true;
      for (int i = 2; i * i <= x; i++) {
        if (x % i == 0) {
          is_prime = false;
        }
      }
      x++;
    }
    return x - 1;
  }
 
  // Function to find the number
  static int findN(int D)
  {
    // Assuming 1+D as first required
    // divisor because it is
    // at distance D from 1
    int a = 1 + D;
 
    // Checking whether 1+D is prime or not
    // otherwise it will return prime number
    // just next to it
    a = next_prime(a);
 
    // Checking the same for next divisor
    int b = a + D;
    b = next_prime(b);
 
    int N = a * b;
    return N;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int D = 2;
 
    int ans = findN(D);
    System.out.println(ans);
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code for the above approach
def next_prime(x):
   
    # Edge case
    if (x == 1 or x == 2):
        return 2
 
    # Checking if x is prime
    is_prime = False
 
    # Loop until next prime is found
    while (not is_prime):
        is_prime = True
        for i in range(2, int(x ** 0.5) + 1):
            if (x % i == 0):
                is_prime = False
 
        x += 1
 
    return x - 1
 
# Function to find the number
def findN(D):
 
    # Assuming 1+D as first required
    # divisor because it is
    # at distance D from 1
    a = 1 + D
 
    # Checking whether 1+D is prime or not
    # otherwise it will return prime number
    # just next to it
    a = next_prime(a)
 
    # Checking the same for next divisor
    b = a + D
    b = next_prime(b)
 
    N = a * b
    return N
 
# Driver Code
D = 2
 
ans = findN(D)
print(ans)
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
class GFG {
 
  static int next_prime(int x)
  {
     
    // Edge case
    if (x == 1 || x == 2) {
      return 2;
    }
 
    // Checking if x is prime
    bool is_prime = false;
 
    // Loop until next prime is found
    while (!is_prime) {
      is_prime = true;
      for (int i = 2; i * i <= x; i++) {
        if (x % i == 0) {
          is_prime = false;
        }
      }
      x++;
    }
    return x - 1;
  }
 
  // Function to find the number
  static int findN(int D)
  {
     
    // Assuming 1+D as first required
    // divisor because it is
    // at distance D from 1
    int a = 1 + D;
 
    // Checking whether 1+D is prime or not
    // otherwise it will return prime number
    // just next to it
    a = next_prime(a);
 
    // Checking the same for next divisor
    int b = a + D;
    b = next_prime(b);
 
    int N = a * b;
    return N;
  }
 
  // Driver Code
  public static void Main () {
    int D = 2;
 
    int ans = findN(D);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
15

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