All Euler problems
Project Euler

Circular Primes

The number 197 is called a circular prime because all rotations of its digits are prime: 197, 971, and 719. How many circular primes are there below one million?

Source sync Apr 19, 2026
Problem #0035
Level Level 01
Solved By 93,511
Languages C++, Python
Answer 55
Length 631 words
number_theorymodular_arithmeticbrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

The number, \(197\), is called a circular prime because all rotations of the digits: \(197\), \(971\), and \(719\), are themselves prime.

There are thirteen such primes below \(100\): \(2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79\), and \(97\).

How many circular primes are there below one million?

Problem 35: Circular Primes

Mathematical Development

Definition 1 (Cyclic rotation). For a kk-digit positive integer nn with decimal representation a1a2aka_1 a_2 \ldots a_k (where a10a_1 \ne 0), the cyclic rotation operator RR is defined by

R(n)=(nmod10)10k1+n/10.R(n) = (n \bmod 10) \cdot 10^{k-1} + \lfloor n / 10 \rfloor.

The orbit of nn under RR is Orb(n)={n,R(n),R2(n),,Rk1(n)}\mathrm{Orb}(n) = \{n, R(n), R^2(n), \ldots, R^{k-1}(n)\}.

Lemma 1 (Rotation formula). The operator RR maps n=10q+rn = 10q + r (with q=n/10q = \lfloor n/10 \rfloor, r=nmod10r = n \bmod 10) to r10k1+qr \cdot 10^{k-1} + q. Applying RR exactly kk times returns the original number: Rk(n)=nR^k(n) = n.

Proof. The representation n=10q+rn = 10q + r has digit string a1a2ak1ra_1 a_2 \ldots a_{k-1} r where qq represents a1a2ak1a_1 a_2 \ldots a_{k-1}. The map RR moves the last digit rr to the leading position: R(n)=r10k1+q=ra1a2ak1R(n) = r \cdot 10^{k-1} + q = r\, a_1\, a_2 \ldots a_{k-1}.

For the periodicity claim, observe that RR acts as a cyclic permutation on the digit string a1a2akaka1ak1a_1 a_2 \ldots a_k \mapsto a_k a_1 \ldots a_{k-1}. Since cyclic permutations of kk elements have order kk, RkR^k is the identity. \square

Definition 2 (Circular prime). A prime pp is circular if every element of Orb(p)\mathrm{Orb}(p) is prime.

Lemma 2 (Single-digit circular primes). The single-digit circular primes are 2,3,5,72, 3, 5, 7.

Proof. A single-digit number nn has Orb(n)={n}\mathrm{Orb}(n) = \{n\}. The single-digit primes are exactly {2,3,5,7}\{2, 3, 5, 7\}, each trivially circular. \square

Theorem 1 (Digit restriction). If pp is a circular prime with k2k \ge 2 digits, then every digit of pp belongs to {1,3,7,9}\{1, 3, 7, 9\}.

Proof. We show that no digit of pp can be 0,2,4,5,60, 2, 4, 5, 6, or 88.

Even digits (0,2,4,6,80, 2, 4, 6, 8): Suppose digit aia_i is even for some 1ik1 \le i \le k. The rotation Rki(p)R^{k-i}(p) places aia_i in the units position, yielding an even number. Since k2k \ge 2, this number is 10\ge 10, hence an even number greater than 2, which is composite. This contradicts Rki(p)R^{k-i}(p) being prime.

Digit 5: Suppose ai=5a_i = 5. Then Rki(p)R^{k-i}(p) ends in 5 and is 10>5\ge 10 > 5, hence divisible by 5 but not equal to 5. This is composite, a contradiction.

Digit 0: Suppose ai=0a_i = 0. Then Rki(p)R^{k-i}(p) has leading digit 0, making it a number with fewer than kk digits (or zero). But all rotations of a kk-digit prime should be kk-digit primes, since p<10kp < 10^k implies all rotations are <10k< 10^k, and a rotation with leading zero has value <10k1< 10^{k-1}, contradicting the expectation that it equals a specific kk-digit rotation. More directly, Rki(p)=010k1+q<10k1R^{k-i}(p) = 0 \cdot 10^{k-1} + q' < 10^{k-1}, which could cause the subsequent rotation formula (which assumes kk digits) to malfunction. In any case, a number with leading digit 0 is not considered a kk-digit number in standard notation.

Therefore, all digits must be odd and not 0 or 5, leaving {1,3,7,9}\{1, 3, 7, 9\}. \square

Theorem 2 (Sieve of Eratosthenes). For any positive integer NN, the sieve of Eratosthenes correctly identifies all primes in {2,3,,N1}\{2, 3, \ldots, N-1\} in O(NloglogN)O(N \log \log N) time using O(N)O(N) space.

Proof. (Standard.) The sieve initializes a boolean array of size NN, then for each prime pNp \le \lfloor\sqrt{N}\rfloor, marks all multiples p2,p2+p,p^2, p^2 + p, \ldots as composite. Correctness follows from the fact that every composite m<Nm < N has a prime factor mN\le \sqrt{m} \le \sqrt{N}. The time complexity is pNN/p=O(NloglogN)\sum_{p \le N} N/p = O(N \log \log N) by Mertens’ second theorem. \square

Theorem 3 (Correctness of the rotation-check algorithm). Given a prime sieve up to NN, the following procedure correctly identifies all circular primes below NN: for each prime p<Np < N, compute all kk rotations of pp via Lemma 1; accept pp as circular if and only if every rotation rr satisfies 0r<N0 \le r < N and rr is marked prime.

Proof. If pp is circular, then all rotations are prime and (since p<Np < N and all rotations of a kk-digit number are also kk-digit numbers with the same digit set) all rotations are <N< N. Conversely, if some rotation rr is composite or rNr \ge N, then pp is not circular. The sieve provides exact primality answers for all integers in [0,N)[0, N), so the procedure is correct. \square

Editorial

We first sieve all primes below the bound so that primality tests for rotations are constant-time lookups. Then we scan the primes in increasing order, form every cyclic rotation of the decimal expansion of each prime, and declare the prime circular only if every rotation also lies below the bound and remains prime. The final count is the number of primes that satisfy this rotation test.

Pseudocode

Algorithm: Counting Circular Primes Below a Bound
Require: An integer N ≥ 2.
Ensure: The number of primes below N whose every decimal rotation is also prime.
1: Build a sieve and a prime lookup structure on {2, 3, ..., N - 1}.
2: Initialize count ← 0.
3: For each prime p < N, generate all cyclic rotations of the decimal expansion of p.
4: If every rotation belongs to the prime set, increment count.
5: Return count.

Complexity Analysis

Proposition. For N=106N = 10^6, the algorithm runs in O(NloglogN)O(N \log \log N) time and O(N)O(N) space.

Proof. The sieve dominates at O(NloglogN)O(N \log \log N). The rotation check iterates over all primes p<Np < N; by the prime-counting function, there are π(N)N/lnN78,498\pi(N) \approx N / \ln N \approx 78{,}498 primes below 10610^6. For each prime, at most k15k - 1 \le 5 rotations are computed in O(1)O(1) each. Thus the rotation phase costs O(π(N)dmax)=O(N/lnN6)=o(N)O(\pi(N) \cdot d_{\max}) = O(N / \ln N \cdot 6) = o(N), dominated by the sieve. Space is O(N)O(N) for the boolean sieve array. \square

Answer

55\boxed{55}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_035/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    const int N = 1000000;
    vector<bool> is_prime(N, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i * i < N; i++)
        if (is_prime[i])
            for (int j = i * i; j < N; j += i)
                is_prime[j] = false;

    int count = 0;
    for (int p = 2; p < N; p++) {
        if (!is_prime[p]) continue;
        int k = 0, tmp = p;
        while (tmp > 0) { k++; tmp /= 10; }
        int pw = 1;
        for (int i = 0; i < k - 1; i++) pw *= 10;
        bool circular = true;
        int r = p;
        for (int i = 0; i < k; i++) {
            if (r < 0 || r >= N || !is_prime[r]) { circular = false; break; }
            r = (r % 10) * pw + r / 10;
        }
        if (circular) count++;
    }
    cout << count << endl;  // 55
    return 0;
}