📌  相关文章
📜  数字中的质数计数

📅  最后修改于: 2021-05-04 07:54:32             🧑  作者: Mango

给定整数N ,任务是计算N中的质数位数。

例子:

方法:想法是遍历数字的所有数字,并检查数字是否为质数。由于在[0,9]范围内只有四个可能的质数,并且确定每个数字都在该范围内,因此我们只需要检查等于集合{ 2,3,5中的任何一个元素的位数,7}

以下是此方法的实现:

CPP
// C++ program to count the number of
// prime digits in a number
  
#include 
using namespace std;
  
// Function to find the count of
// prime digits in a number
int countDigit(int n)
{
    int temp = n, count = 0;
  
    // Loop to compute all the digits
    // of the number
    while (temp != 0) {
  
        // Finding every digit of the
        // given number
        int d = temp % 10;
  
        temp /= 10;
  
        // Checking if digit is prime or not
        // Only 2, 3, 5 and 7 are prime
        // one-digit number
        if (d == 2 || d == 3
            || d == 5 || d == 7)
            count++;
    }
  
    return count;
}
  
// Driver code
int main()
{
    int n = 1234567890;
  
    cout << countDigit(n) << endl;
    return 0;
}


Java
// Java program to count the number of
// prime digits in a number
class GFG {
      
    // Function to find the count of
    // prime digits in a number
    static int countDigit(int n)
    {
        int temp = n, count = 0;
      
        // Loop to compute all the digits
        // of the number
        while (temp != 0) {
      
            // Finding every digit of the
            // given number
            int d = temp % 10;
      
            temp /= 10;
      
            // Checking if digit is prime or not
            // Only 2, 3, 5 and 7 are prime
            // one-digit number
            if (d == 2 || d == 3
                || d == 5 || d == 7)
                count++;
        }
      
        return count;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int n = 1234567890;
      
        System.out.println(countDigit(n)) ;
  
    }
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 program to count the number of
# prime digits in a number
  
# Function to find the count of
# prime digits in a number
def countDigit(n):
    temp = n
    count = 0
  
    # Loop to compute all the digits
    # of the number
    while (temp != 0):
  
        # Finding every digit of the
        # given number
        d = temp % 10
  
        temp //= 10
  
        # Checking if digit is prime or not
        # Only 2, 3, 5 and 7 are prime
        # one-digit number
        if (d == 2 or d == 3 or d == 5 or d == 7):
            count += 1
  
    return count
  
# Driver code
if __name__ == '__main__':
    n = 1234567890
  
    print(countDigit(n))
  
# This code is contributed by mohit kumar 29


C#
// C# program to count the number of
// prime digits in a number
using System;
  
class GFG {
      
    // Function to find the count of
    // prime digits in a number
    static int countDigit(int n)
    {
        int temp = n, count = 0;
      
        // Loop to compute all the digits
        // of the number
        while (temp != 0) {
      
            // Finding every digit of the
            // given number
            int d = temp % 10;
      
            temp /= 10;
      
            // Checking if digit is prime or not
            // Only 2, 3, 5 and 7 are prime
            // one-digit number
            if (d == 2 || d == 3
                || d == 5 || d == 7)
                count++;
        }
      
        return count;
    }
      
    // Driver code
    public static void Main (string[] args)
    {
        int n = 1234567890;
      
        Console.WriteLine(countDigit(n)) ;
    }
}
  
// This code is contributed by AnkitRai01


输出:
4

时间复杂度: O(N) ,其中N是数字的长度。