All Euler problems
Project Euler

Digit Permutation Primes

A digit permutation prime group is a set of primes that are all permutations of the same digits. Find the number of such groups among primes below 10^6 where the group size is at least 4.

Source sync Apr 19, 2026
Problem #0983
Level Level 39
Solved By 52
Languages C++, Python
Answer 984.
Length 203 words
combinatoricsnumber_theorybrute_force

Problem Statement

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

We say two circles on the plane harmonise if the circles intersect at two grid pointsA point with integer coordinates, in which case the two intersection points are called the harmony points.

A set of circles on the plane is called consonant if it satisfies all the following requirements:

  1. There are at least two circles in the set.
  2. The center point of every circle is a grid point.
  3. All circles have the same radius.
  4. No circle is tangent to any other circle.
  5. The circles are connected in the sense that a chain of circles can be formed between every pair of circles such that each circle harmonises with the next circle.

It can be proven that the number of unique harmony points of a consonant set of circles cannot be smaller than the number of circles. If the number of unique harmony points equals the number of circles, we say the consonant set is perfect.

For example, here are two perfect consonant sets of circles:

0983_circles.png

Let be the minimal radius such that a perfect consonant set of or more circles with radius exists.
You are given and .

Find .

Problem 983: Digit Permutation Primes

Mathematical Analysis

Grouping by Digit Signature

Two primes belong to the same group iff their sorted digit tuples are identical. For example, 1487, 4817, 8147 share the digit set {1,4,7,8}\{1, 4, 7, 8\}.

Proposition. All primes in a permutation group have the same number of digits and the same digit sum (hence the same residue modulo 9).

Density Estimate

Among π(106)=78498\pi(10^6) = 78498 primes, 6-digit primes (the majority) have roughly 6!/(digit repeats)6! / (\text{digit repeats}) potential permutations, but most are not prime. Groups of size 4\ge 4 are relatively rare.

Derivation

Editorial

Count the number of “digit permutation groups” of size >= 4 among primes below 10^6. A digit permutation group is a set of primes that are anagrams of each other (i.e., they share the same multiset of digits). We count how many such groups contain at least 4 primes. Key observations:. We sieve primes below 10610^6. We then iterate over each prime, compute sorted digit tuple as key. Finally, group by key using a hash map.

Pseudocode

Sieve primes below $10^6$
For each prime, compute sorted digit tuple as key
Group by key using a hash map
Count groups with $\ge 4$ members

Complexity Analysis

O(NloglogN)O(N \log \log N) sieve + O(π(N)logN)O(\pi(N) \log N) for grouping.

Answer

984.\boxed{984.}

Code

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

C++ project_euler/problem_983/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    const int N = 1000000;
    vector<bool> is_p(N, true); is_p[0]=is_p[1]=false;
    for(int i=2;(long long)i*i<N;i++) if(is_p[i]) for(int j=i*i;j<N;j+=i) is_p[j]=false;
    map<string,int> groups;
    for(int p=2;p<N;p++){
        if(!is_p[p]) continue;
        string s=to_string(p); sort(s.begin(),s.end());
        groups[s]++;
    }
    int cnt=0;
    for(auto&[k,v]:groups) if(v>=4) cnt++;
    cout<<cnt<<endl;
    return 0;
}