📜  将数字相除的数字的素数位数

📅  最后修改于: 2021-09-04 08:14:51             🧑  作者: Mango

给定一个整数N ,任务是计算 N 中的位数,N 是一个质数,并除以这个数字。
例子:

朴素的方法:这个想法是找到数字的所有数字。对于每个数字,检查是否是素数。如果是,则检查它是否除以数字。如果两种情况都为真,则将计数加 1。最终计数是所需的答案。
有效的方法:由于只有2、3、5和 7 是素数个位数,因此对于每个数字,检查是否可以整除该数字,如果是 2、3、5 或 7。如果两种情况都为真,则将计数加 1。最终计数是要求的答案。
下面是这个方法的实现:

C++
// C++ program to count number of digits
// which is prime and also divides number
 
#include 
 
using namespace std;
 
// Function to find the number of
// digits in number which divides the
// number and is also a prime number
int countDigit(int n)
{
    bool prime[10];
    memset(prime, false, sizeof(prime));
 
    // Only 2, 3, 5 and 7 are prime
    // one-digit number
    prime[2] = prime[3] = true;
    prime[5] = prime[7] = true;
 
    int temp = n, count = 0;
     
    // Loop to compute all the digits
    // of the number untill it
    // is not equal to the zero
    while (temp != 0) {
 
        // Fetching each digit
        // of the number
        int d = temp % 10;
 
        temp /= 10;
 
        // Checking if digit is greater than 0
        // and can divides n and is prime too
        if (d > 0 && n % d == 0 && prime[d])
            count++;
    }
 
    return count;
}
 
// Driven Code
int main()
{
    int n = 1032;
 
    cout << countDigit(n) << endl;
    return 0;
}


Java
// Java program to count number of digits
// which is prime and also divides number
class GFG {
     
    // Function to find the number of
    // digits in number which divides the
    // number and is also a prime number
    static int countDigit(int n)
    {
        boolean prime[]  = new boolean[10];
         
        for (int i = 0; i < 10; i++)
            prime[i] = false;
 
        // Only 2, 3, 5 and 7 are prime
        // one-digit number
        prime[2] = prime[3] = true;
        prime[5] = prime[7] = true;
     
        int temp = n, count = 0;
         
        // Loop to compute all the digits
        // of the number untill it
        // is not equal to the zero
        while (temp != 0) {
     
            // Fetching each digit
            // of the number
            int d = temp % 10;
     
            temp /= 10;
     
            // Checking if digit is greater than 0
            // and can divides n and is prime too
            if (d > 0 && n % d == 0 && prime[d] == true)
                count++;
        }
     
        return count;
    }
     
    // Driven Code
    public static void main (String[] args)
    {
        int n = 1032;
     
        System.out.println(countDigit(n)) ;
    }
}
 
// This code is contributed by Yash_R


Python3
# Python program to count number of digits
# which is prime and also divides number
 
# Function to find the number of
# digits in number which divides the
# number and is also a prime number
def countDigit(n):
    prime = [False]*10
 
    # Only 2, 3, 5 and 7 are prime
    # one-digit number
    prime[2] = True
    prime[3] = True;
    prime[5] = True
    prime[7] = True;
 
    temp = n
    count = 0;
     
    # Loop to compute all the digits
    # of the number untill it
    # is not equal to the zero
    while (temp != 0):
         
        # Fetching each digit
        # of the number
        d = temp % 10;
 
        temp //= 10;
 
        # Checking if digit is greater than 0
        # and can divides n and is prime too
        if (d > 0 and n % d == 0 and prime[d]):
            count += 1
 
    return count
 
# Driver Code
n = 1032
 
print(countDigit(n))
 
# This code is contributed by ANKITKUMAR34


C#
// C# program to count number of digits
// which is prime and also divides number
using System;
 
class GFG {
     
    // Function to find the number of
    // digits in number which divides the
    // number and is also a prime number
    static int countDigit(int n)
    {
        bool []prime  = new bool[10];
         
        for (int i = 0; i < 10; i++)
            prime[i] = false;
 
        // Only 2, 3, 5 and 7 are prime
        // one-digit number
        prime[2] = prime[3] = true;
        prime[5] = prime[7] = true;
     
        int temp = n, count = 0;
         
        // Loop to compute all the digits
        // of the number untill it
        // is not equal to the zero
        while (temp != 0) {
     
            // Fetching each digit
            // of the number
            int d = temp % 10;
     
            temp /= 10;
     
            // Checking if digit is greater than 0
            // and can divides n and is prime too
            if (d > 0 && n % d == 0 && prime[d] == true)
                count++;
        }
     
        return count;
    }
     
    // Driven Code
    public static void Main (string[] args)
    {
        int n = 1032;
     
        Console.WriteLine(countDigit(n)) ;
    }
}
 
// This code is contributed by Yash_R


Javascript


输出:
2

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live