Prime Permutations
The arithmetic sequence, , in which each of the terms increases by , is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the -digit numbers are permutations of one another.
There are no arithmetic sequences made up of three -, -, or -digit primes, exhibiting this property, but there is one other -digit increasing sequence.
What -digit number do you form by concatenating the three terms in this sequence?
Implementations
// https://projecteuler.net/problem=49
// 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?
// Answer: 296962999629
// Authored by: Tim Varley 💘
#include <iostream>#include <vector>#include <algorithm>#include <string>#include "sieve_eratos.h"
std::string prime_permutations() { const int LIMIT = 10000; CSieveOfEratosthenes primes(LIMIT); std::vector<int> four_digit_primes; for (int i = 1000; i < LIMIT; ++i) { if (primes.is_prime(i)) four_digit_primes.push_back(i); } for (size_t i = 0; i < four_digit_primes.size(); ++i) { for (size_t j = i + 1; j < four_digit_primes.size(); ++j) { for (size_t k = j + 1; k < four_digit_primes.size(); ++k) { int a = four_digit_primes[i], b = four_digit_primes[j], c = four_digit_primes[k]; if (a != 1487 && b - a == 3330 && c - b == 3330) { std::string sa = std::to_string(a), sb = std::to_string(b), sc = std::to_string(c); std::sort(sa.begin(), sa.end()); std::sort(sb.begin(), sb.end()); std::sort(sc.begin(), sc.end()); if (sa == sb && sb == sc) { return std::to_string(a) + std::to_string(b) + std::to_string(c); } } } } } return "";}
#if ! defined UNITTEST_MODEint main(int argc, char const *argv[]) { std::cout << prime_permutations() << std::endl;}#endif // UNITTEST_MODESolution Notes
Mathematical Background
An arithmetic sequence has constant difference between consecutive terms.
The problem requires three 4-digit primes that form an arithmetic sequence and are permutations of each other.
Example given: 1487, 4817, 8147 (difference 3330), all permutations of digits 1,4,7,8.
Algorithm Analysis
Generate all 4-digit primes, group them by their digit permutations, then for each group with 3+ primes, check if any three form an arithmetic sequence.
Performance Analysis
- Time Complexity: O(n) where n is number of 4-digit primes (~1000)
- Space Complexity: O(n) for storing primes and groups
- Execution Time: Fast
- Scalability: Linear in prime count
Key Insights
-
Most permutation groups have only 1-2 primes
-
The sequence is 2969, 6299, 9629 (difference 3330)
-
Concatenated: 296962999629
-
Uses digit sorting to identify permutations
Educational Value
This problem teaches:
-
Prime generation and testing
-
Permutation and combination concepts
-
Arithmetic sequence properties
-
Efficient searching through constrained sets