A Circular Prime is a special type of prime number in which every number obtained by cyclically rotating its digits is also a prime number. It is an interesting programming concept that combines prime number verification, digit extraction, mathematical operations and number manipulation. In Java, a Circular Prime can be identified by repeatedly moving the last digit of a number to the beginning and checking whether each resulting number is prime. Let us understand the concept, develop the algorithm, implement the Java program and examine the logic behind digit rotation.
What Is a Circular Prime?
A positive integer is called a Circular Prime if the number itself and all the numbers obtained by cyclically rotating its digits are prime. For example, consider the number 197. Its cyclic rotations are:
- 197
- 719
- 971
All three numbers are prime. Therefore, 197 is a Circular Prime. Consider another example, 23. Its rotations are:
- 23 – Prime
- 32 – Not Prime
Since one of the rotations is not prime, 23 is not a Circular Prime.
Algorithm to Check a Circular Prime
The program follows a systematic approach to generate all cyclic rotations and check their primality.
Step-by-step algorithm:
- Start.
- Accept a positive integer n from the user.
- Calculate the total number of digits in the given number.
- Store the original number in a variable named temp.
- Initialize a Boolean variable flag to true.
- Repeat the following steps for the total number of digits:
- Check whether the current value of temp is prime.
- If it is prime, display the number along with the result.
- Otherwise, display that it is not prime and set flag to false.
- Extract the last digit using temp % 10.
- Remove the last digit using temp / 10.
- Place the extracted digit at the beginning to generate the next rotation.
- After checking all rotations, examine the value of flag.
- If flag is true, display that the given number is a Circular Prime.
- Otherwise, display that it is not a Circular Prime.
- Stop
Java Program to Check a Circular Prime
The following program uses separate methods for checking prime numbers and generating circular rotations. This makes the program easier to understand, test and maintain.
import java.util.Scanner;
class CircularPrime
{
// Method to check prime number
boolean isPrime(int n)
{
if(n < 2)
return false;
for(int i = 2; i <= Math.sqrt(n); i++)
{
if(n % i == 0)
return false;
}
return true;
}
// Method to check Circular Prime
void check(int n)
{
int temp = n;
int digits = 0;
boolean flag = true;
// Count the number of digits
int x = n;
while(x > 0)
{
digits++;
x = x / 10;
}
System.out.println(“Rotations and their results:”);
for(int i = 1; i <= digits; i++)
{
System.out.print(temp + ” : “);
if(isPrime(temp))
System.out.println("Prime");
else
{
System.out.println("Not Prime");
flag = false;
}
// Generate the next rotation
int last = temp % 10;
temp = temp / 10;
temp = last * (int)Math.pow(10, digits - 1)+ temp;
}
if(flag)
System.out.println(n + " is a Circular Prime.");
else
System.out.println(n + " is not a Circular Prime.");
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
CircularPrime ob = new CircularPrime();
System.out.print("Enter a positive integer: ");
int n = sc.nextInt();
if(n > 0)
ob.check(n);
else
System.out.println("Invalid Input!");
sc.close();
}
}
Explanation of the methods:
- isPrime(int n): Checks whether the supplied number is prime by testing its divisibility.
- check(int n): Counts the digits, generates successive rotations and checks their primality.
- main(): Accepts the number from the user and invokes the required method.
The Boolean variable flag keeps track of whether all rotations are prime.
Sample Executions
Execution 1: Circular Prime
Enter a positive integer: 197
Rotations and their results:
197 : Prime
719 : Prime
971 : Prime
197 is a Circular Prime.
Explanation: The number 197 generates three rotations: 197, 719 and 971. Since all three numbers are prime, the original number is classified as a Circular Prime.
Execution 2: Not a Circular Prime
Enter a positive integer: 23
Rotations and their results:
23 : Prime
32 : Not Prime
23 is not a Circular Prime.
Explanation: Although 23 is prime, its rotation 32 is not prime because it is divisible by 2. Therefore, 23 is not a Circular Prime.
Understanding the Digit Rotation Logic
The most important part of this program is the logic used to move the last digit of a number to its first position. Consider the following Java statements:
int last = temp % 10;
temp = temp / 10;
temp = last * (int)Math.pow(10, digits – 1) + temp;
Let us understand these statements using the number 197.
Step 1: Extract the last digit
last = 197 % 10;
Result: last = 7
The modulus operator (%) returns the remainder when a number is divided by 10. Therefore, it extracts the last digit.
Step 2: Remove the last digit
temp = 197 / 10;
Result:
temp = 19
Integer division removes the last digit of the number.
Step 3: Move the extracted digit to the beginning
temp = 7 * 100 + 19;
Result:
temp = 719
Here, Math.pow(10, digits – 1) calculates the required power of 10. Since the number contains three digits, the extracted digit is multiplied by \(10^2\), or 100.
Complete rotation process:
| Iteration | Current Number | Next Rotation |
| 1 | 197 | 719 |
| 2 | 719 | 971 |
| 3 | 971 | 197 |
The process is repeated for the total number of digits. After three rotations, the original number is obtained again.
Important Concepts Used in the Program
| Java Concept | Purpose |
| % operator | Extracts the last digit |
| / operator | Removes the last digit |
| Math.sqrt() | Limits the prime-checking loop |
| Math.pow() | Calculates the positional value of a digit |
| Boolean variable | Tracks whether all rotations are prime |
| for loop | Repeats the rotation process |
| User-defined methods | Separates prime checking from circular prime checking |
Why does the prime-checking loop run only up to the square root of a number?
If a number has a factor greater than its square root, it must have a corresponding factor smaller than its square root. Therefore, checking divisibility beyond the square root is unnecessary.
For example, to check whether 97 is prime, it is sufficient to test divisibility by integers from 2 to 9.
Important Examination Notes
Students should pay particular attention to the following points while writing this program in their practical examination:
- The original number must be included in the prime-checking process.
- The total number of digits must be calculated before generating rotations.
- The number of rotations must be equal to the number of digits.
- The last digit must be extracted before removing it.
- The extracted digit must be multiplied by the appropriate power of 10.
- The Boolean variable must become false whenever any rotation is not prime.
Additional programming consideration: The given implementation is intended for ordinary positive integers without leading-zero rotations. For example, rotating 101 produces 110, not 011 as an integer. Also, for very large inputs, a more careful implementation should avoid possible integer overflow.
Conclusion
The Circular Prime program is a useful practical examination question because it combines multiple fundamental Java programming concepts, including loops, conditional statements, user-defined methods, Boolean variables, prime number checking and digit manipulation.
Understanding the rotation logic is particularly important because the same technique can be applied to several other number-based programming problems involving cyclic permutations and digit rearrangement.