给定数字N,任务是检查它是否是可置换的质数。
可置换素数是在通过任何置换切换数字位置之后也是素数的数字。一些可置换素数是2、3、5、7、11等。
先决条件:原始性测试| CPP next_permute()
例子 :
Input : 31
Output : Yes
Explanation :
Both 13 and 31 are prime.
Input : 19
Output : No
Explanation :
19 is prime but 91 is not
方法 :
1)构造Eratosthenes筛,以有效地找到素数。
2)要么通过切换数字来生成数字的每个排列,要么使用内置的C++函数next_permutation来检查其是否为质数
3)如果数字的排列不是素数,则简单地回答是“否”,否则回答“是”。
下面是上述方法的实现。
C++
// CPP Program to check whether number is
// permutable prime or not
#include
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
= 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