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.
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.
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 firstname.lastname@example.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.
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if a number is divisible by all prime divisors of another number
- Check if a number is Primorial Prime or not
- Check if the first and last digit of number N is prime and their sum is less than K
- Check a number for Permutable Prime
- Check if a number is Quartan Prime or not
- Check whether the given number is Wagstaff prime or not
- Check if a number is a Pythagorean Prime or not
- Check whether N is a Dihedral Prime Number or not
- Check if a number is Full Prime
- C Program to Check Whether a Number is Prime or not
- Check if a number can be written as a sum of 'k' prime numbers
- Python program to check whether a number is Prime or not
- Check if the first and last digit of the smallest number forms a prime
- Check if there exists a prime number which gives Y after being repeatedly subtracted from X