Check Circular Prime in Java
Problem
A circular prime is a prime number where every rotation of its digits — moving digits from the front to the back — produces another prime number.
Given a number, determine whether it is a circular prime.
Java Program
public class CircularPrimeCheck {
static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; (long) i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
int n = 197;
String digits = String.valueOf(n);
boolean isCircular = true;
for (int i = 0; i < digits.length(); i++) {
String rotated = digits.substring(i) + digits.substring(0, i); // moves the first i digits to the end
if (!isPrime(Integer.parseInt(rotated))) {
isCircular = false;
break; // found a non-prime rotation, no need to check the rest
}
}
System.out.println("Circular prime: " + isCircular);
}
}Output
Core Logic
Generating every rotation of the number's digits as a string, and checking that each one is prime, confirms the circular property directly.
- 1
digitsholds the number's digits as aString, so they can be sliced and rearranged easily. - 2For each rotation index
i,digits.substring(i) + digits.substring(0, i)builds the digits movediplaces, wrapping the front around to the back. - 3Each rotated string is parsed back into an
intand tested with the same square-root-boundisPrime()check. - 4The first rotation that fails primality sets
isCirculartofalseand exits the loop immediately withbreak.
197, the three rotations are 197, 971, and 719 — all three are prime, so isCircular stays true.Key Point: Every rotation of a number has the same digits, just reordered, so this only ever needs to check as many rotations as the number has digits — a 3-digit number needs exactly 3 checks.
Why: Each of the d digit-rotations gets its own O(√n) primality check, and building each rotated string costs space proportional to the number of digits.
Key Concepts
Approach 2: Java 8
import java.util.stream.IntStream;
public class CircularPrimeCheckStream {
static boolean isPrime(int n) {
if (n < 2) return false;
return IntStream.rangeClosed(2, (int) Math.sqrt(n)).noneMatch(i -> n % i == 0);
}
public static void main(String[] args) {
int n = 197;
String digits = String.valueOf(n);
// Every rotation of the digits must itself be prime
boolean isCircular = IntStream.range(0, digits.length())
.mapToObj(i -> digits.substring(i) + digits.substring(0, i))
.allMatch(rotated -> isPrime(Integer.parseInt(rotated)));
System.out.println("Circular prime: " + isCircular);
}
}
Output
Core Logic
The same rotate-and-check idea can be expressed as a stream — map each rotation index to its rotated number, and require every one to be prime.
- 1
IntStream.range(0, digits.length())generates every rotation index. - 2
.mapToObj(i -> digits.substring(i) + digits.substring(0, i))maps each index to its rotated digit string, the same formula the loop version uses. - 3
.allMatch(rotated -> isPrime(Integer.parseInt(rotated)))parses and primality-checks every rotation, requiring all of them to pass.
197, the stream generates "197", "971", and "719", and allMatch() confirms all three are prime.Key Point: allMatch() short-circuits at the first non-prime rotation found, the same early-exit behavior as the loop's break.
Why: The stream still builds and primality-checks each of the d rotated strings, the same total work as the manual loop.