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...
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 and , write if and have the same multiset of decimal digits. Define the canonical signature as the string obtained by sorting the digits of in non-decreasing order.
Lemma 1 (Characterization of ). if and only if .
Proof. Two multisets are equal if and only if their sorted representations coincide. The function produces the sorted representation of the digit multiset.
Definition 2 (Arithmetic progression of primes with digit permutation). A triple is an AP-permutation triple if:
- are all prime,
- (arithmetic progression with common difference ),
- (mutual digit permutations).
Lemma 2 (Bound on common difference). For a 4-digit AP-permutation triple , the common difference satisfies .
Proof. All three terms must be 4-digit numbers, so and . Thus , giving .
Lemma 3 (Grouping strategy). If is an AP-permutation triple, then . Hence all members of the triple belong to the same -equivalence class.
Proof. Immediate from condition (3) of Definition 2 and Lemma 1.
Theorem 1 (Exhaustive enumeration). The only 4-digit AP-permutation triples are:
- with common difference , and
- with common difference .
Proof. Step 1. Generate all 4-digit primes via the Sieve of Eratosthenes. There are such primes.
Step 2. Partition into equivalence classes under using the canonical signature . For each class with , enumerate all ordered pairs with , and check whether lies in (this is the arithmetic progression condition ).
Step 3. The enumeration yields exactly two triples:
- , with .
- , with .
Verification of the second triple:
- is prime: ; not divisible by any prime up to .
- is prime: ; not divisible by any prime up to .
- is prime: ; not divisible by any prime up to .
- Arithmetic: .
- Digits: .
Since the exhaustive search over all pairs within each group is complete, no other triple exists.
Corollary 1. The answer (excluding the known triple starting at 1487) is the concatenation .
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 and compute the only possible third term of an arithmetic progression, namely . A set lookup determines whether this third value is present in the same permutation class, and excluding the known sequence beginning with 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 time, where , is the number of 4-digit primes, and is the size of the largest equivalence class.
Proof. The sieve of Eratosthenes on costs . Computing for each 4-digit prime costs where (sorting 4 digits), which is . For each equivalence class of size , the nested loop examines pairs with set lookup each. The total over all classes is . In practice, , so this is .
Proposition (Space complexity). for storing the grouped primes and the set lookup structures.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 49: Prime Permutations
Find the other 3-term arithmetic sequence of 4-digit primes that are
permutations of each other, and concatenate the three terms.
"""
from collections import defaultdict
def sieve(limit):
is_prime = [True] * limit
is_prime[0] = is_prime[1] = False
for i in range(2, int(limit**0.5) + 1):
if is_prime[i]:
for j in range(i * i, limit, i):
is_prime[j] = False
return is_prime
def solve():
is_prime = sieve(10000)
groups = defaultdict(list)
for p in range(1000, 10000):
if is_prime[p]:
groups[''.join(sorted(str(p)))].append(p)
for primes in groups.values():
if len(primes) < 3:
continue
pset = set(primes)
for i in range(len(primes)):
for j in range(i + 1, len(primes)):
third = 2 * primes[j] - primes[i]
if third in pset and primes[i] != 1487:
return f"{primes[i]}{primes[j]}{third}"
print(solve())