Prime Permutations¶

An easy way to approach this problem is to first find arithmetic progressions of primes, then check if any of those progressions have numbers that are permutations of each other.

To start, we'll generate all the four-digit primes.

In [1]:
primes = prime_range(1000, 10000)

Here's where we find arithmetic progressions. Given two primes $p < q$, we can calculate their difference $d = q - p$ and check if $q + d$ is prime - if it is, we have a three-number progression.

In [2]:
from itertools import combinations

progressions = set()
for (p, q) in combinations(primes, 2):
    d = q - p
    r = q + d
    if r < 10000 and is_prime(r):
        progressions.add((p, q, r))

Here's how many of these progressions there are.

In [3]:
len(progressions)
Out[3]:
42994

Now we'll check each progression to find one where the digits of the numbers are permutations of each other.

In [4]:
for (p, q, r) in progressions:
    if p == 1487 and q == 4817 and r == 8147:
        continue
        
    if sorted(p.digits()) == sorted(q.digits()) == sorted(r.digits()):
        break
        
p, q, r
Out[4]:
(2969, 6299, 9629)

Copyright (C) 2025 filifa¶

This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license and the BSD Zero Clause license.