Pointer-Prime Number es un número primo p tal que el próximo primo después de p se puede obtener de p sumando el producto de los dígitos de p.
Algunos números primos punteros son:
23, 61, 1123, 1231, 1321, 2111, 2131, 11261….
Compruebe si N es un número primo de Pointer
Dado un número N , la tarea es comprobar si N es un Número Primo Puntero o no. Si N es un número primo de puntero, imprima «Sí» , de lo contrario, imprima «No» .
Ejemplos:
Entrada: N = 23
Salida: Sí
Explicación:
23 + producto de dígitos de 23 = 29,
que es el siguiente número primo después de 23.
Entrada: N = 29
Salida: No
Enfoque: :
- Encuentra el producto de los dígitos de N
- Luego, encuentre el siguiente número primo a N
- Ahora, si N es primo y N + el producto de los dígitos de N es igual al próximo primo de N, entonces imprima «Sí» , de lo contrario, imprima «No» .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation for the // above approach #include <bits/stdc++.h> using namespace std; // Function to find the product of // digits of a number N int digProduct(int n) { int product = 1; while (n != 0) { product = product * (n % 10); n = n / 10; } return product; } // Function that returns true if n // is prime else returns false bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to return the smallest // prime number greater than N int nextPrime(int N) { // Base case if (N <= 1) return 2; int prime = N; bool found = false; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } // Function to check Pointer-Prime numbers bool isPointerPrime(int n) { if (isPrime(n) && (n + digProduct(n) == nextPrime(n))) return true; else return false; } // Driver Code int main() { // Given Number N int N = 23; // Function Call if (isPointerPrime(N)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java program for above approach class GFG{ // Function to find the product of // digits of a number N static int digProduct(int n) { int product = 1; while (n != 0) { product = product * (n % 10); n = n / 10; } return product; } // Function that returns true if n // is prime else returns false static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to return the smallest // prime number greater than N static int nextPrime(int N) { // Base case if (N <= 1) return 2; int prime = N; boolean found = false; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } // Function to check Pointer-Prime numbers static boolean isPointerPrime(int n) { if (isPrime(n) && (n + digProduct(n) == nextPrime(n))) return true; else return false; } // Driver Code public static void main(String[] args) { // Given Number N int N = 23; // Function Call if (isPointerPrime(N)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by Shubham Prakash
Python3
# Python3 implementation for the above approach def digProduct(n): product = 1 while(n != 0): product = product * (n % 10) n = int(n / 10) return product # Function that returns true if n # is prime else returns false def isPrime(n): # Corner cases if (n <= 1): return False if (n <= 3): return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 == 0 or n % 3 == 0): return False i = 5 while(i * i <= n): if (n % i == 0 or n % (i + 2) == 0): return False i = i + 6 return True # Function to return the smallest prime # number greater than N def nextPrime(N): # Base case if(N <= 1): return 2; prime = N found = False # Loop continuously until isPrime # returns true for a number greater # than n while(not found): prime = prime + 1 if(isPrime(prime)): found = True return prime # Function to check Pointer-Prime numbers def isPointerPrime(n): if(isPrime(n) and (n + digProduct(n) == nextPrime(n))): return True else: return False # Driver Code if __name__=="__main__": # Given number N N = 23 # Function call if(isPointerPrime(N)): print("Yes") else: print("No") # This code is contributed by adityakumar27200
C#
// C# program for above approach using System; class GFG{ // Function to find the product of // digits of a number N static int digProduct(int n) { int product = 1; while (n != 0) { product = product * (n % 10); n = n / 10; } return product; } // Function that returns true if n // is prime else returns false static bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to return the smallest // prime number greater than N static int nextPrime(int N) { // Base case if (N <= 1) return 2; int prime = N; bool found = false; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } // Function to check Pointer-Prime numbers static bool isPointerPrime(int n) { if (isPrime(n) && (n + digProduct(n) == nextPrime(n))) return true; else return false; } // Driver Code public static void Main() { // Given Number N int N = 23; // Function Call if (isPointerPrime(N)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by Code_Mech
Javascript
<script> // Javascript implementation for the // above approach // Function to find the product of // digits of a number N function digProduct(n) { var product = 1; while (n != 0) { product = product * (n % 10); n = parseInt(n / 10); } return product; } // Function that returns true if n // is prime else returns false function isPrime(n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (var i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to return the smallest // prime number greater than N function nextPrime(N) { // Base case if (N <= 1) return 2; var prime = N; var found = false; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } // Function to check Pointer-Prime numbers function isPointerPrime(n) { if (isPrime(n) && (n + digProduct(n) == nextPrime(n))) return true; else return false; } // Driver Code // Given Number N var N = 23; // Function Call if (isPointerPrime(N)) document.write( "Yes"); else document.write( "No"); </script>
Producción:
Yes
Complejidad temporal: O(n).