Open In App

Count of prime digits of a Number which divides the number

Last Updated : 20 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to count the number of digits in N which is a prime number, and also divides the number.
Examples: 
 

Input: N = 12 
Output:
Explanation: 
Digits of the number = {1, 2} 
But, only 2 is prime number that divides N.
Input: N = 1032 
Output:
Explanation: 
Digits of the number = {1, 0, 3, 2} 
3 and 2 divides the number and are also prime. 
 

 

Naive Approach: The idea is to find all the digits of the number. For each digit, check if is prime or not. If yes, then check if it divides the number or not. If both the cases are true, then increment the count by 1. The final count is the required answer.
Efficient Approach: Since only 2, 3, 5, and 7 are the prime single-digit numbers, therefore for each digit, check if divides the number and if is 2, 3, 5 or 7. If both the cases are true, then increment the count by 1. The final count is the required answer.
Below is the implementation of this approach:
 

C++




// C++ program to count number of digits
// which is prime and also divides number
 
#include <bits/stdc++.h>
 
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 until 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
import java.io.*;
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 until 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 until 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 until 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




<script>
 
// Javascript 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
function countDigit(n)
{
    var prime = Array(10).fill(false);
 
    // Only 2, 3, 5 and 7 are prime
    // one-digit number
    prime[2] = prime[3] = true;
    prime[5] = prime[7] = true;
 
    var temp = n, count = 0;
     
    // Loop to compute all the digits
    // of the number until it
    // is not equal to the zero
    while (temp != 0) {
 
        // Fetching each digit
        // of the number
        var d = temp % 10;
 
        temp = parseInt(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
    n = 1032;
 
    document.write(countDigit(n));
 
</script>


Output: 

2

 

Time Complexity: O(log10n)
Auxiliary Space: O(prime)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads