Check whether a number is circular prime or not - GeeksforGeeks

30 Sep, 2021
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
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++
Java
Python
C#
PHP
Javascript
// Program to check if a number is circular// prime or not.#include <iostream>#include <cmath>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 Programint main(){    int N = 1193;    if (checkCircular(N))        cout << "Yes" << endl;    else        cout << "No" << endl;    return 0;}
// 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 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 ProgramN = 1193;if (checkCircular(N)) :    print "Yes"else :    print "No"     # This code is contributed by Nikita Tiwari
// 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// Program to check if// a number is circular// prime or not. // Function to check if a// number is prime or not.function 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 ||        $n % 3 == 0)        return false;     for ($i = 5;         $i * $i <= $n; $i = $i + 6)        if ($n % $i == 0 ||            $n % ($i + 2) == 0)            return -1;     return true;} // Function to check if// the number is circular// prime or not.function checkCircular($N){    // Count digits.    $count = 0;    $temp = $N;    while ($temp)    {        $count++;        $temp =(int)$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 = (int)$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 -1;} // Driver Code$N = 1193;if (checkCircular($N))    echo "Yes" ,"\n";else    echo "No" ,"\n";     // This code is contributed// by akt_mit?>
<script>     // Javascript Program to check if a number    // is circular prime or not.         // Function to check if a    // number is prime or not.    function 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 || n % 3 == 0)            return false;          for (let 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.    function checkCircular(N)    {                  // Count digits.        let count = 0, temp = N;        while (temp > 0)        {            count++;            temp = parseInt(temp / 10, 10);        }          let num = N;        while (isPrime(num))        {              // Following three lines generate            // the next circular permutation            // of a number. We move last digit            // to first position.            let rem = num % 10;            let div = parseInt(num / 10, 10);            num = ((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;    }         let N = 1193;    if (checkCircular(N))      document.write("Yes");    else      document.write("No");     </script>
Output: 
Yes
Time Complexity: O(N*N1/2)
Auxiliary Space: O(1)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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
nitin mittal
jit_t
subhammahato348
divyeshrabadiya07
dhanya4
Prime Number
Mathematical
Mathematical
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program for factorial of a number
Print all possible combinations of r elements in a given array of size n
Program for Decimal to Binary Conversion
The Knight's tour problem | Backtracking-1
Operators in C / C++
Find minimum number of coins that make a given value
Program to find sum of elements in a given array