我们得到一个数字n。我们的任务是检查数字是否为素数。
循环质数:如果在数字的任何循环排列之后仍保持质数,则质数被称为循环质数。
例子:
Input : n = 113
Output : Yes
All cyclic permutations of 113 (311
and 131) are prime.
Input : 1193
Output : Yes
这个想法很简单,我们生成给定数字的所有排列并检查每个排列的质数。为了生成排列,我们将最后一位逐个移动到第一个位置。
C++
// Program to check if a number is circular
// prime or not.
#include
#include
using namespace std;
// Function to check if a number is prime or not.
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 check if the number is circular
// prime or not.
bool checkCircular(int N)
{
// Count digits.
int count = 0, temp = N;
while (temp) {
count++;
temp /= 10;
}
int num = N;
while (isPrime(num)) {
// Following three lines generate the next
// circular permutation of a number. We move
// last digit to first position.
int rem = num % 10;
int div = num / 10;
num = (pow(10, count - 1)) * rem + div;
// If all the permutations are checked and
// we obtain original number exit from loop.
if (num == N)
return true;
}
return false;
}
// Driver Program
int main()
{
int N = 1193;
if (checkCircular(N))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
Java
// Java Program to check if a number
// is circular prime or not.
import java.lang.*;
class GFG
{
// Function to check if a number is prime or not.
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 check if the number is circular
// prime or not.
static boolean checkCircular(int N)
{
// Count digits.
int count = 0, temp = N;
while (temp>0) {
count++;
temp /= 10;
}
int num = N;
while (isPrime(num)) {
// Following three lines generate the next
// circular permutation of a number. We
// move last digit to first position.
int rem = num % 10;
int div = num / 10;
num = (int)((Math.pow(10, count - 1)) * rem)
+ div;
// If all the permutations are checked and
// we obtain original number exit from loop.
if (num == N)
return true;
}
return false;
}
// Driver Program
public static void main (String[] args)
{
int N = 1193;
if (checkCircular(N))
System.out.println("Yes");
else
System.out.println("No");
}
}
/* This code is contributed by Kriti Shukla */
Python
# Python Program to check if a number
# is circular prime or not.
import math
# Function to check if a number is prime
# or not.
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 check if the number is
# circular prime or not.
def checkCircular(N) :
#Count digits.
count = 0
temp = N
while (temp > 0) :
count = count + 1
temp = temp / 10
num = N;
while (isPrime(num)) :
# Following three lines generate the
# next circular permutation of a
# number. We move last digit to
# first position.
rem = num % 10
div = num / 10
num = (int)((math.pow(10, count - 1))
* rem)+ div
# If all the permutations are checked
# and we obtain original number exit
# from loop.
if (num == N) :
return True
return False
# Driver Program
N = 1193;
if (checkCircular(N)) :
print "Yes"
else :
print "No"
# This code is contributed by Nikita Tiwari
C#
// C# Program to check if a number
// is circular prime or not.
using System;
class GFG {
// Function to check if a
// number is prime or not.
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 check if the number
// is circular prime or not.
static bool checkCircular(int N)
{
// Count digits.
int count = 0, temp = N;
while (temp > 0)
{
count++;
temp /= 10;
}
int num = N;
while (isPrime(num))
{
// Following three lines generate
// the next circular permutation
// of a number. We move last digit
// to first position.
int rem = num % 10;
int div = num / 10;
num = (int)((Math.Pow(10, count -
1)) * rem) + div;
// If all the permutations are
// checked and we obtain original
// number exit from loop.
if (num == N)
return true;
}
return false;
}
// Driver code
public static void Main ()
{
int N = 1193;
if (checkCircular(N))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Nitin Mittal.
PHP
输出:
Yes
时间复杂度: O(N 1/2 )