📌  相关文章
📜  能被所有可能的素数整除的最小 N 位数

📅  最后修改于: 2021-10-26 06:58:00             🧑  作者: Mango

给定一个整数N ,任务是找到可以被所有可能的素数整除的最小N位数,即2, 3, 57 。如果没有这样的数字,则打印-1
例子:

方法:
请按照以下步骤解决问题:

  • 由于所有四个数字2, 3, 5, 7都是素数,这意味着N也可以被它们的乘积整除2 × 3 × 5 × 7 = 210
  • 对于N < 3 ,不存在这样的数字。所以,打印-1
  • 对于N = 3 ,答案将是210
  • 对于N > 3 ,需要进行以下计算:
    • 求余数R = 10 N-1 % N。
    • 210 – R添加到10 N-1

下面是上述方法的实现:

C++
// C++ implementation of the above approach 
#include 
using namespace std;
 
// Function to find the minimum number of 
// n digits divisible by all prime digits
void minNum(int n)
{
    if (n < 3)
        cout << -1;
    else
        cout << (210 * ((int)(pow(10, n - 1) /
                                   210) + 1));
}
 
// Driver Code
int main()
{
    int n = 5;
    minNum(n);
    return 0;
}
 
// This code is contributed by amal kumar choubey


Java
// Java implementation of the above approach
class GFG{
     
// Function to find the minimum number of
// n digits divisible by all prime digits
static void minNum(int n)
{
    if(n < 3)
        System.out.println(-1);
    else
        System.out.println(210 * (
            (int)(Math.pow(10, n - 1) / 210) + 1));
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5;
    minNum(n);
}
}
 
// This code is contributed by Stuti Pathak


Python3
# Python3 implementation of the above approach
from math import *
  
# Function to find the minimum number of
# n digits divisible by all prime digits.
def minNum(n):
    if n < 3:
        print(-1)
    else:
        print(210 * (10**(n-1) // 210 + 1))
  
# Driver Code
n = 5
minNum(n)


C#
// C# implementation of the above approach
using System;
 
class GFG{
 
// Function to find the minimum number of
// n digits divisible by all prime digits
static void minNum(int n)
{
    if (n < 3)
        Console.WriteLine(-1);
    else
        Console.WriteLine(210 *
           ((int)(Math.Pow(10, n - 1) / 210) + 1));
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5;
    minNum(n);
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
10080

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