Conteo de dígitos primos de un Número que divide al número

Dado un número entero N , la tarea es contar el número de dígitos en N, que es un número primo, y también divide el número.
Ejemplos: 
 

Entrada: N = 12 
Salida:
Explicación: 
Dígitos del número = {1, 2} 
Pero, solo 2 es un número primo que divide a N.
Entrada: N = 1032 
Salida:
Explicación: 
Dígitos del número = {1, 0 , 3, 2} 
3 y 2 dividen el número y también son primos. 
 

Enfoque ingenuo: la idea es encontrar todos los dígitos del número. Para cada dígito, comprueba si es primo o no. En caso afirmativo, compruebe si divide el número o no. Si ambos casos son verdaderos, entonces incremente el conteo en 1. El conteo final es la respuesta requerida.
Enfoque eficiente: dado que solo 2, 3, 5 y 7 son números primos de un solo dígito, por lo tanto, para cada dígito, verifique si divide el número y si es 2, 3, 5 o 7. Si ambos casos son verdaderos, entonces incrementa el conteo en 1. El conteo final es la respuesta requerida.
A continuación se muestra la implementación de este enfoque:
 

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
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>
Producción: 

2

 

Complejidad de tiempo: O (log 10 n)

Espacio auxiliar: O (principal)

Publicación traducida automáticamente

Artículo escrito por muskan_garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *