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++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000001
void sieveOfEratosthenes( bool * primes)
{
primes[1] = false ;
for ( int i = 2; i * i < MAX; i++) {
if (primes[i] == true ) {
for ( int j = i * 2; j < MAX; j += i)
primes[j] = false ;
}
}
}
bool checkPermutablePrime( int N)
{
bool primes[MAX];
memset (primes, true , sizeof (primes));
sieveOfEratosthenes(primes);
int num[7];
int pos = 0;
while (N > 0) {
num[pos++] = N % 10;
N /= 10;
}
int SZ = pos;
int flag = 0;
sort(num, num + SZ);
do {
int temp = 0;
pos = 0;
while (pos < SZ) {
temp = temp * 10 + num[pos];
pos++;
}
if (primes[temp] == false ) {
flag = 1;
break ;
}
} while (next_permutation(num, num + SZ));
if (flag)
return false ;
return true ;
}
int main()
{
int N = 31;
cout << (checkPermutablePrime(N) == 1 ?
"Yes" : "No" ) << endl;
N = 19;
cout << (checkPermutablePrime(N) == 1 ?
"Yes" : "No" ) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class PermutablePrime {
static final int MAX = 1000001 ;
static boolean [] primes = new boolean [MAX];
static void sieveOfEratosthenes() {
Arrays.fill(primes, true );
primes[ 1 ] = false ;
for ( int i = 2 ; i * i < MAX; i++) {
if (primes[i]) {
for ( int j = i * 2 ; j < MAX; j += i) {
primes[j] = false ;
}
}
}
}
static boolean checkPermutablePrime( int N) {
Arrays.fill(primes, true );
sieveOfEratosthenes();
int [] num = new int [ 7 ];
int pos = 0 ;
while (N > 0 ) {
num[pos++] = N % 10 ;
N /= 10 ;
}
int SZ = pos;
int flag = 0 ;
Arrays.sort(num, 0 , SZ);
do {
int temp = 0 ;
pos = 0 ;
while (pos < SZ) {
temp = temp * 10 + num[pos];
pos++;
}
if (!primes[temp]) {
flag = 1 ;
break ;
}
} while (nextPermutation(num, SZ));
if (flag == 1 ) {
return false ;
}
return true ;
}
static boolean nextPermutation( int [] num, int SZ) {
int i = SZ - 2 ;
while (i >= 0 && num[i] >= num[i + 1 ]) {
i--;
}
if (i == - 1 ) {
return false ;
}
int j = SZ - 1 ;
while (num[j] <= num[i]) {
j--;
}
int temp = num[i];
num[i] = num[j];
num[j] = temp;
int left = i + 1 , right = SZ - 1 ;
while (left < right) {
temp = num[left];
num[left++] = num[right];
num[right--] = temp;
}
return true ;
}
public static void main(String[] args)
{
int N = 31 ;
System.out.println(checkPermutablePrime(N) ? "Yes" : "No" );
N = 19 ;
System.out.println(checkPermutablePrime(N) ? "Yes" : "No" );
}
}
|
Python3
import itertools
MAX = 1000001
def sieveOfEratosthenes(primes):
primes[ 1 ] = False ;
for i in range ( 2 , int ( MAX * * 0.5 ) + 1 ):
if (primes[i] = = True ):
for j in range (i + i, MAX , i):
primes[j] = False ;
return primes
def checkPermutablePrime(N):
primes = [ True for _ in range ( MAX )]
primes = sieveOfEratosthenes(primes);
num = list ( map ( int , str (N)))
permutations = list (itertools.permutations(num));
ind = 0
flag = 0
for num in permutations:
temp = int ("".join( list ( map ( str , num))))
if (primes[temp] = = False ):
flag = 1 ;
break ;
return not flag
N = 31 ;
print ([ "No" , "Yes" ][checkPermutablePrime(N) = = 1 ])
N = 19 ;
print ([ "No" , "Yes" ][checkPermutablePrime(N) = = 1 ])
|
C#
using System;
class PermutablePrime {
static readonly int MAX = 1000001;
static bool [] primes = new bool [MAX];
static void SieveOfEratosthenes()
{
Array.Fill(primes, true );
primes[1] = false ;
for ( int i = 2; i * i < MAX; i++) {
if (primes[i]) {
for ( int j = i * 2; j < MAX; j += i) {
primes[j] = false ;
}
}
}
}
static bool CheckPermutablePrime( int N)
{
Array.Fill(primes, true );
SieveOfEratosthenes();
int [] num = new int [7];
int pos = 0;
while (N > 0) {
num[pos++] = N % 10;
N /= 10;
}
int SZ = pos;
int flag = 0;
Array.Sort(num, 0, SZ);
do {
int temp = 0;
pos = 0;
while (pos < SZ) {
temp = temp * 10 + num[pos];
pos++;
}
if (!primes[temp]) {
flag = 1;
break ;
}
} while (NextPermutation(num, SZ));
if (flag == 1) {
return false ;
}
return true ;
}
static bool NextPermutation( int [] num, int SZ)
{
int i = SZ - 2;
while (i >= 0 && num[i] >= num[i + 1]) {
i--;
}
if (i == -1) {
return false ;
}
int j = SZ - 1;
while (num[j] <= num[i]) {
j--;
}
int temp = num[i];
num[i] = num[j];
num[j] = temp;
int left = i + 1, right = SZ - 1;
while (left < right) {
temp = num[left];
num[left++] = num[right];
num[right--] = temp;
}
return true ;
}
public static void Main( string [] args)
{
int N = 31;
Console.WriteLine(CheckPermutablePrime(N) ? "Yes"
: "No" );
N = 19;
Console.WriteLine(CheckPermutablePrime(N) ? "Yes"
: "No" );
}
}
|
PHP
<?php
$MAX = 100001;
$zz ;
$l = 0;
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 ;
}
function sieveOfEratosthenes( $primes )
{
global $MAX ;
$primes [1] = false;
for ( $i = 2; $i * $i < $MAX ; $i ++)
{
if ( $primes [ $i ] == true)
{
for ( $j = $i * 2;
$j < $MAX ; $j += $i )
$primes [ $j ] = false;
}
}
return $primes ;
}
function checkPermutablePrime( $N )
{
global $MAX , $zz , $l ;
$primes = array_fill (0, $MAX , true);
$primes = sieveOfEratosthenes( $primes );
$num = array ();
$pos = 0;
while ( $N > 0)
{
$num [ $pos ++] = $N % 10;
$N = (int)( $N / 10);
}
$flag = 0;
sort( $num );
$num1 = next_permutation( $num );
$i = 0;
while ( $i < count ( $num1 ))
{
$temp = 0;
$pos = 0;
while ( $pos < strlen ( $num1 [ $i ]))
{
$temp = $temp * 10 +
ord( $num1 [ $i ][ $pos ]) - 48;
$pos ++;
}
if ( $primes [ $temp ] == false)
{
$flag = 1;
break ;
}
$i ++;
}
$zz = array ();
$l = 0;
if ( $flag )
return false;
return true;
}
$N = 31;
echo (checkPermutablePrime( $N ) == 1 ?
"Yes" : "No" ) . "\n" ;
$N = 19;
echo (checkPermutablePrime( $N ) == 1 ?
"Yes" : "No" ) . "\n" ;
?>
|
Javascript
let MAX = 1000001
const permutator = (nums) => {
let permutations = [];
const permute = (arr, m = []) => {
if (arr.length === 0) {
permutations.push(m)
} else {
for (let i = 0; i < arr.length; i++) {
let curr = arr.slice();
let next = curr.splice(i, 1);
permute(curr.slice(), m.concat(next))
}
}
}
permute(nums)
return permutations;
}
function sieveOfEratosthenes(primes)
{
primes[1] = false ;
for ( var i = 2; i * i < MAX; i++) {
if (primes[i] == true ) {
for ( var j = i * 2; j < MAX; j += i)
primes[j] = false ;
}
}
}
function checkPermutablePrime(N)
{
let primes = new Array(MAX).fill( true );
sieveOfEratosthenes(primes);
let num = new Array(7);
let pos = 0;
while (N > 0) {
num[pos++] = N % 10;
N = Math.floor(N / 10);
}
let SZ = pos;
let flag = 0;
num.sort();
var ind = 0;
let permutations = permutator(num);
do {
let temp = 0;
pos = 0;
while (pos < SZ) {
temp = temp * 10 + num[pos];
pos++;
}
if (primes[temp] == false ) {
flag = 1;
break ;
}
num = permutations[ind++];
} while (ind < permutations.length)
if (flag)
return false ;
return true ;
}
let N = 31;
console.log((checkPermutablePrime(N) == 1 ?
"Yes" : "No" ));
N = 19;
console.log((checkPermutablePrime(N) == 1 ?
"Yes" : "No" ));
|
Time Complexity : O(n * log(log(n)) + n!). The sieve of Eratosthenes has a time complexity of O(n * log(log(n))) for finding all primes up to a given number, and the checking for permutations of the digits has a time complexity of O(n!), where n is the number of digits in the input number.
space complexity :O(n), where n is the number of integers up to MAX. This is because the program uses an array of size MAX to store the prime numbers found by the Sieve of Eratosthenes.
Last Updated :
06 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...