# Check whether a number is circular prime or not

We are given a number n. Our task is to check whether the number is circular prime or not.

Circular Prime : A prime number is said to be a circular prime if after any cyclic permutations of the digits, it remains a prime.

Examples:

```Input :  n = 113
Output : Yes
All cyclic permutations of 113 (311
and 131) are prime.

Input : 1193
Output : Yes
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is simple, we generate all permutations of given number and check each permutation for prime. To generate permutations, we one by one move last digit to first position.

## 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

 ` `

Output:

```Yes
```

Optimizations:
Clearly numbers containing 0, 2, 4, 5, 6, or 8 can never be circular primes as numbers ending with these will always be divisible by 2 or 5. Hence one of their permutation will not be a prime. While counting digits in first step, we can also check if current digit is one of these.

This article is contributed by Vineet Joshi. 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.