Dado un número entero N , la tarea es contar el número de dígitos primos en N .
Ejemplos:
Entrada: N = 12
Salida: 1
Explicación:
Dígitos del número: {1, 2}
Pero, solo 2 es un número primo.
Entrada: N = 1032
Salida: 2
Explicación:
Dígitos del número: {1, 0, 3, 2}
3 y 2 son números primos
Enfoque: La idea es iterar a través de todos los dígitos del número y verificar si el dígito es primo o no. Dado que solo hay cuatro números primos posibles en el rango [0, 9] y cada dígito seguramente se encuentra en este rango, solo necesitamos verificar el número de dígitos igual a cualquiera de los elementos en el conjunto {2, 3, 5 , 7} .
A continuación se muestra la implementación de este enfoque:
CPP
// C++ program to count the number of // prime digits in a number #include <bits/stdc++.h> 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
Javascript
<script> // JavaScript program to count the number of // prime digits in a number // Function to find the count of // prime digits in a number function countDigit(n) { let temp = n, count = 0; // Loop to compute all the digits // of the number while (temp != 0) { // Finding every digit of the // given number let d = temp % 10; temp = Math.floor(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 let n = 1234567890; document.write(countDigit(n) + "<br>"); // This code is contributed by gfgking </script>
4
Complejidad de tiempo: O(log 10 N) , donde N es la longitud del número.
Espacio Auxiliar: O(1)
Método n. ° 2: usar recursividad
C++
// C++ program to count the number of // prime digits in a number #include <bits/stdc++.h> using namespace std; // Function to find the count of // prime digits in a number int countDigit(int n,int count=0) { //BASE CASE if (n==0) return count; // Finding every digit of the // given number int d = n % 10; n /= 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++;} countDigit(n,count);//recursively calling function } // Driver code int main() { int n = 1234567890; cout << countDigit(n) << endl; return 0; } //This code is contributed by Shivesh Kumar Dwivedi
4