Check a number for Permutable Prime
Given a number N, task is to Check whether it is a permutable prime number or not.
A Permutable prime number is that number which after switching the position of digits through any permutation is also prime. Some of the permutable prime numbers are 2, 3, 5, 7, 11, etc.
Prerequisites : Primality Test | CPP next_permute()
Examples :
Input : 31 Output : Yes Explanation : Both 13 and 31 are prime. Input : 19 Output : No Explanation : 19 is prime but 91 is not
Approach :
1) Construct Sieve of Eratosthenes to find the prime numbers efficiently.
2) Either generate every permutation of the number by switching its digits or use inbuilt C++ function next_permutation to check whether it is prime
3) If any permutation of digits is not prime, simply answer is NO, otherwise YES.
Below is the implementation of above approach.
C++
// CPP Program to check whether number is // permutable prime or not #include <bits/stdc++.h> using namespace std; #define MAX 1000001 // Sieve of Eratosthenes to find the // prime numbers upto MAX efficiently void sieveOfEratosthenes(bool* primes) { // 1 is neither prime nor composite primes[1] = false; for (int i = 2; i * i < MAX; i++) { // If prime[i] is not changed, // then it is a prime if (primes[i] == true) { // Update all multiples of i for (int j = i * 2; j < MAX; j += i) primes[j] = false; } } } // Function returns 1 if the number N is // permutable prime otherwise not bool checkPermutablePrime(int N) { // Boolean Array for prime numbers bool primes[MAX]; // Initialize all entries as true. // A value in prime[i] will finally // be false if i is not a prime, // else true. memset(primes, true, sizeof(primes)); sieveOfEratosthenes(primes); // Creating Array to store digits int num[7]; // Convert the number into array of digits int pos = 0; while (N > 0) { num[pos++] = N % 10; N /= 10; } // Size of Array int SZ = pos; int flag = 0; sort(num, num + SZ); do { // Construct the number again // from array of digits int temp = 0; pos = 0; while (pos < SZ) { temp = temp * 10 + num[pos]; pos++; } // check if it is prime of not if (primes[temp] == false) { flag = 1; break; } } while (next_permutation(num, num + SZ)); // If flag is 1, number // is not permutable prime if (flag) return false; return true; } // Driver Code to check above functions int main() { int N = 31; cout << (checkPermutablePrime(N) == 1 ? "Yes" : "No") << endl; N = 19; cout << (checkPermutablePrime(N) == 1 ? "Yes" : "No") << endl; return 0; } |
PHP
<?php // PHP Program to check // whether number is // permutable prime or not $MAX = 100001; $zz; $l = 0; // to find all permutation // of that number function next_permutation($items, $perms = array( )) { global $zz, $l; if (empty($items)) { $zz[$l++] = join($perms); } else { for ($i = count($items) - 1; $i >= 0; --$i) { $newitems = $items; $newperms = $perms; list($foo) = array_splice($newitems, $i, 1); array_unshift($newperms, $foo); next_permutation($newitems, $newperms); } } return $zz; } // Sieve of Eratosthenes to // find the prime numbers // upto MAX efficiently function sieveOfEratosthenes($primes) { global $MAX; // 1 is neither prime // nor composite $primes[1] = false; for ($i = 2; $i * $i < $MAX; $i++) { // If prime[i] is not changed, // then it is a prime if ($primes[$i] == true) { // Update all multiples of i for ($j = $i * 2; $j < $MAX; $j += $i) $primes[$j] = false; } } return $primes; } // Function returns 1 if the // number N is permutable // prime otherwise not function checkPermutablePrime($N) { global $MAX, $zz, $l; // Boolean Array for // prime numbers // Initialize all entries // as true. A value in // prime[i] will finally // be false if i is not a // prime, else true. $primes = array_fill(0, $MAX, true); $primes = sieveOfEratosthenes($primes); // Creating Array // to store digits $num = array(); // Convert the number // into array of digits $pos = 0; while ($N > 0) { $num[$pos++] = $N % 10; $N = (int)($N / 10); } // Size of Array $flag = 0; sort($num); $num1 = next_permutation($num); $i = 0; while ($i < count($num1)) { // Construct the number again // from array of digits $temp = 0; $pos = 0; while ($pos < strlen($num1[$i])) { $temp = $temp * 10 + ord($num1[$i][$pos]) - 48; $pos++; } // check if it is // prime of not if ($primes[$temp] == false) { $flag = 1; break; } $i++; } $zz = array(); $l = 0; // If flag is 1, number // is not permutable prime if ($flag) return false; return true; } // Driver Code $N = 31; echo (checkPermutablePrime($N) == 1 ? "Yes" : "No") . "\n"; $N = 19; echo (checkPermutablePrime($N) == 1 ? "Yes" : "No") . "\n"; // This Code is contributed // by mits ?> |
Yes No
Recommended Posts:
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if a number is divisible by all prime divisors of another number
- Check whether a number is circular prime or not
- Check if the first and last digit of number N is prime and their sum is less than K
- Check whether the given number is Wagstaff prime or not
- Check if a number is Full Prime
- C Program to Check Whether a Number is Prime or not
- Check if a number is a Pythagorean Prime or not
- Check if a number is Primorial Prime or not
- Check whether N is a Dihedral Prime Number or not
- Check if a number is Quartan Prime or not
- Check if a number can be written as a sum of 'k' prime numbers
- Python program to check whether a number is Prime or not
- Check if the first and last digit of the smallest number forms a prime
- Check if LCM of array elements is divisible by a prime number or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar



