📌  相关文章
📜  程序检查数字是否可被任何数字整除

📅  最后修改于: 2021-04-24 04:28:04             🧑  作者: Mango

给定整数N ,其中1 \leq n \leq 10^{18}     。任务是检查数字是否不能被任何数字整除。如果给定数字N可被其任何数字整除,则打印“是”,否则打印“否”。

例子:

Input : N = 5115
Output : YES
Explanation: 5115 is divisible by both 1 and 5.
So print YES.

Input : 27
Output : NO
Explanation: 27 is not divisible by 2 or 7

方法:解决此问题的想法是一一提取数字,然后检查该数字是否可被任何数字整除。如果它可以被任何数字整除,则打印“是”,否则打印“否”。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to check if given number is divisible
// by any of its digits
string isDivisible(long long int n)
{
    long long int temp = n;
 
    // check if any of digit divides n
    while (n) {
        int k = n % 10;
 
        // check if K divides N
        if (temp % k == 0)
            return "YES";
 
        n /= 10;
    }
 
    return "NO";
}
 
// Driver Code
int main()
{
    long long int n = 9876543;
 
    cout << isDivisible(n);
 
    return 0;
}


Java
// Java implementation of above approach
 
class GFG
{
 
    // Function to check if given number is divisible
    // by any of its digits
    static String isDivisible(int n)
    {
        int temp = n;
 
        // check if any of digit divides n
        while (n > 0)
        {
            int k = n % 10;
 
            // check if K divides N
            if (temp % k == 0)
            {
                return "YES";
            }
            n /= 10;
        }
 
        return "NO";
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 9876543;
        System.out.println(isDivisible(n));
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program implementation of above approach
 
# Function to check if given number is
# divisible by any of its digits
def isDivisible(n):
    temp = n
 
    # check if any of digit divides n
    while(n):
        k = n % 10
 
        # check if K divides N
        if(temp % k == 0):
            return "YES"
 
        n /= 10;
 
    # Number is not divisible by
    # any of digits
    return "NO"
 
# Driver Code
n = 9876543
print(isDivisible(n))
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# implementation of above approach
using System;
 
class GFG
{
 
    // Function to check if given number is divisible
    // by any of its digits
    static String isDivisible(int n)
    {
        int temp = n;
 
        // check if any of digit divides n
        while (n > 0)
        {
            int k = n % 10;
 
            // check if K divides N
            if (temp % k == 0)
            {
                return "YES";
            }
            n /= 10;
        }
 
        return "NO";
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 9876543;
        Console.WriteLine(isDivisible(n));
    }
}
 
// This code is contributed by PrinciRaj1992


PHP


Javascript


Python3
# Python implementation of above approach
def getResult(n):
   
    # Converting integer to string
    st = str(n)
     
    # Traversing the string
    for i in st:
       
        # If the number is divisible by
        # digits then return yes
        if(n % int(i) == 0):
            return 'Yes'
           
    # If no digits are dividing the
    # number then return no
    return 'No'
 
 
# Driver Code
n = 9876543
 
# passing this number to get result function
print(getResult(n))
# this code is contributed by vikkycirus


输出:
YES

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

方法2:使用字符串:

  • 我们必须通过采用新变量将给定的数字转换为字符串。
  • 遍历字符串,
  • 将字符转换为整数(数字)
  • 检查数字是否可以被任何数字整除,然后打印“是”,否则打印“否”。

下面是上述方法的实现:

Python3

# Python implementation of above approach
def getResult(n):
   
    # Converting integer to string
    st = str(n)
     
    # Traversing the string
    for i in st:
       
        # If the number is divisible by
        # digits then return yes
        if(n % int(i) == 0):
            return 'Yes'
           
    # If no digits are dividing the
    # number then return no
    return 'No'
 
 
# Driver Code
n = 9876543
 
# passing this number to get result function
print(getResult(n))
# this code is contributed by vikkycirus

输出:

Yes