All Euler problems
Project Euler

Prime Permutations

The arithmetic sequence 1487, 4817, 8147, in which each term increases by 3330, is unusual in two ways: (i) each of the three terms is prime, and (ii) each of the three 4-digit numbers are permutat...

Source sync Apr 19, 2026
Problem #0049
Level Level 02
Solved By 64,570
Languages C++, Python
Answer 296962999629
Length 518 words
number_theorycombinatoricssequence

Problem Statement

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

The arithmetic sequence, \(1487, 4817, 8147\), in which each of the terms increases by \(3330\), is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the \(4\)-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three \(1\)-, \(2\)-, or \(3\)-digit primes, exhibiting this property, but there is one other \(4\)-digit increasing sequence.

What \(12\)-digit number do you form by concatenating the three terms in this sequence?

Problem 49: Prime Permutations

Mathematical Development

Formal Development

Definition 1 (Digit permutation equivalence). For non-negative integers aa and bb, write aba \sim b if aa and bb have the same multiset of decimal digits. Define the canonical signature σ(n)\sigma(n) as the string obtained by sorting the digits of nn in non-decreasing order.

Lemma 1 (Characterization of \sim). aba \sim b if and only if σ(a)=σ(b)\sigma(a) = \sigma(b).

Proof. Two multisets are equal if and only if their sorted representations coincide. The function σ\sigma produces the sorted representation of the digit multiset. \square

Definition 2 (Arithmetic progression of primes with digit permutation). A triple (p1,p2,p3)(p_1, p_2, p_3) is an AP-permutation triple if:

  1. p1<p2<p3p_1 < p_2 < p_3 are all prime,
  2. p2p1=p3p2p_2 - p_1 = p_3 - p_2 (arithmetic progression with common difference d=p2p1d = p_2 - p_1),
  3. p1p2p3p_1 \sim p_2 \sim p_3 (mutual digit permutations).

Lemma 2 (Bound on common difference). For a 4-digit AP-permutation triple (p1,p2,p3)(p_1, p_2, p_3), the common difference satisfies d4499d \leq 4499.

Proof. All three terms must be 4-digit numbers, so 1000p11000 \leq p_1 and p3=p1+2d9999p_3 = p_1 + 2d \leq 9999. Thus 2d99991000=89992d \leq 9999 - 1000 = 8999, giving d4499d \leq 4499. \square

Lemma 3 (Grouping strategy). If (p1,p2,p3)(p_1, p_2, p_3) is an AP-permutation triple, then σ(p1)=σ(p2)=σ(p3)\sigma(p_1) = \sigma(p_2) = \sigma(p_3). Hence all members of the triple belong to the same σ\sigma-equivalence class.

Proof. Immediate from condition (3) of Definition 2 and Lemma 1. \square

Theorem 1 (Exhaustive enumeration). The only 4-digit AP-permutation triples are:

  1. (1487,4817,8147)(1487, 4817, 8147) with common difference d=3330d = 3330, and
  2. (2969,6299,9629)(2969, 6299, 9629) with common difference d=3330d = 3330.

Proof. Step 1. Generate all 4-digit primes P4={p:1000p9999, p prime}\mathcal{P}_4 = \{p : 1000 \leq p \leq 9999,\ p \text{ prime}\} via the Sieve of Eratosthenes. There are P4=π(9999)π(999)=1061|\mathcal{P}_4| = \pi(9999) - \pi(999) = 1061 such primes.

Step 2. Partition P4\mathcal{P}_4 into equivalence classes under \sim using the canonical signature σ\sigma. For each class GG with G3|G| \geq 3, enumerate all ordered pairs (pi,pj)G×G(p_i, p_j) \in G \times G with pi<pjp_i < p_j, and check whether pk:=2pjpip_k := 2p_j - p_i lies in GG (this is the arithmetic progression condition pkpj=pjpip_k - p_j = p_j - p_i).

Step 3. The enumeration yields exactly two triples:

  • σ(1487)=σ(4817)=σ(8147)=“1478”\sigma(1487) = \sigma(4817) = \sigma(8147) = \text{``1478''}, with d=3330d = 3330.
  • σ(2969)=σ(6299)=σ(9629)=“2699”\sigma(2969) = \sigma(6299) = \sigma(9629) = \text{``2699''}, with d=3330d = 3330.

Verification of the second triple:

  • 29692969 is prime: 2969=54\lfloor\sqrt{2969}\rfloor = 54; not divisible by any prime up to 5454.
  • 62996299 is prime: 6299=79\lfloor\sqrt{6299}\rfloor = 79; not divisible by any prime up to 7979.
  • 96299629 is prime: 9629=98\lfloor\sqrt{9629}\rfloor = 98; not divisible by any prime up to 9898.
  • Arithmetic: 62992969=3330=962962996299 - 2969 = 3330 = 9629 - 6299. \checkmark
  • Digits: σ(2969)=σ(6299)=σ(9629)=“2699”\sigma(2969) = \sigma(6299) = \sigma(9629) = \text{``2699''}. \checkmark

Since the exhaustive search over all (10612)\binom{1061}{2} pairs within each group is complete, no other triple exists. \square

Corollary 1. The answer (excluding the known triple starting at 1487) is the concatenation 296962999629296962999629.

Editorial

We generate all four-digit primes and group them by their sorted-digit signatures, so each group contains precisely the prime permutations of one another. Inside each group, we inspect ordered pairs pi<pjp_i < p_j and compute the only possible third term of an arithmetic progression, namely pk=2pjpip_k = 2p_j - p_i. A set lookup determines whether this third value is present in the same permutation class, and excluding the known sequence beginning with 14871487 leaves the required triple.

Pseudocode

Algorithm: Arithmetic Prime Permutation Sequence
Require: The four-digit primes.
Ensure: The 12-digit concatenation of the nontrivial three-term arithmetic progression formed by prime permutations.
1: Generate all four-digit primes and group them by their sorted-digit signatures.
2: For each signature class with at least three members, sort the primes in increasing order.
3: For each ordered pair p < q in the class, set r ← 2q - p and test whether r also lies in the class.
4: If such a triple exists and is not the known example 1487, 4817, 8147, return the concatenation of p, q, and r.

Complexity Analysis

Proposition (Time complexity). The algorithm runs in O(NloglogN+π4gmax2)O(N \log \log N + \pi_4 \cdot g_{\max}^2) time, where N=9999N = 9999, π4=1061\pi_4 = 1061 is the number of 4-digit primes, and gmaxg_{\max} is the size of the largest equivalence class.

Proof. The sieve of Eratosthenes on [0,N][0, N] costs O(NloglogN)O(N \log \log N). Computing σ(p)\sigma(p) for each 4-digit prime costs O(π4dlogd)O(\pi_4 \cdot d \log d) where d=4d = 4 (sorting 4 digits), which is O(π4)O(\pi_4). For each equivalence class of size gg, the nested loop examines (g2)\binom{g}{2} pairs with O(1)O(1) set lookup each. The total over all classes is G(G2)π4gmax\sum_{G} \binom{|G|}{2} \leq \pi_4 \cdot g_{\max}. In practice, gmax15g_{\max} \leq 15, so this is O(π4)O(\pi_4). \square

Proposition (Space complexity). O(π4)O(\pi_4) for storing the grouped primes and the set lookup structures.

Answer

296962999629\boxed{296962999629}

Code

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

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

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

    map<string, vector<int>> groups;
    for (int p = 1000; p < LIMIT; p++) {
        if (!is_prime[p]) continue;
        string s = to_string(p);
        sort(s.begin(), s.end());
        groups[s].push_back(p);
    }

    for (auto& [key, primes] : groups) {
        if ((int)primes.size() < 3) continue;
        set<int> pset(primes.begin(), primes.end());
        for (int i = 0; i < (int)primes.size(); i++) {
            for (int j = i + 1; j < (int)primes.size(); j++) {
                int third = 2 * primes[j] - primes[i];
                if (third < LIMIT && pset.count(third) && primes[i] != 1487) {
                    cout << primes[i] << primes[j] << third << endl;
                    return 0;
                }
            }
        }
    }
    return 0;
}