Java ProgramsNumbersCheck Circular Prime

Check Circular Prime in Java

intermediate·  Numbers  ·  Number Theory

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.

Input
197
Output
Circular prime: true

Java Program

Java
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

Circular prime: true

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.

How It Works
  1. 1digits holds the number's digits as a String, so they can be sliced and rearranged easily.
  2. 2For each rotation index i, digits.substring(i) + digits.substring(0, i) builds the digits moved i places, wrapping the front around to the back.
  3. 3Each rotated string is parsed back into an int and tested with the same square-root-bound isPrime() check.
  4. 4The first rotation that fails primality sets isCircular to false and exits the loop immediately with break.
For 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.

Complexity
Time Complexity: O(d × √n)Space Complexity: O(d)

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

digit rotationString.substring()early exit with break

Approach 2: Java 8

Java
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

Circular prime: true

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.

How It Works
  1. 1IntStream.range(0, digits.length()) generates every rotation index.
  2. 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. 3.allMatch(rotated -> isPrime(Integer.parseInt(rotated))) parses and primality-checks every rotation, requiring all of them to pass.
For 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.

Complexity
Time Complexity: O(d × √n)Space Complexity: O(d)

Why: The stream still builds and primality-checks each of the d rotated strings, the same total work as the manual loop.

Key Concepts

StreamIntStream.range()allMatch()

Related Programs