Tim Varley Logo
Tim Varley Systems Engineer
Problem #41 hard

Pandigital Prime

We shall say that an nn-digit number is pandigital if it makes use of all the digits 11 to nn exactly once. For example, 21432143 is a 44-digit pandigital and is also prime.

What is the largest nn-digit pandigital prime that exists?

Implementations

cpp
// https://projecteuler.net/problem=41
// We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
// What is the largest n-digit pandigital prime that exists?
// Answer: 7652413
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
long long pandigital_prime() {
std::string digits = "1234567";
std::sort(digits.begin(), digits.end());
long long max_prime = 0;
do {
long long num = std::stoll(digits);
bool is_prime = true;
if (num <= 1) is_prime = false;
else if (num == 2) is_prime = true;
else if (num % 2 == 0) is_prime = false;
else {
for (long long i = 3; i * i <= num; i += 2) {
if (num % i == 0) {
is_prime = false;
break;
}
}
}
if (is_prime) {
if (num > max_prime) max_prime = num;
}
} while (std::next_permutation(digits.begin(), digits.end()));
return max_prime;
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[]) {
std::cout << pandigital_prime() << std::endl;
}
#endif // UNITTEST_MODE

Solution Notes

Mathematical Background

A number is pandigital if it uses each digit from 1 to n exactly once in an n-digit number. For example, 2143 is a 4-digit pandigital number.

The problem seeks the largest n-digit pandigital prime. Since primes greater than 2 are odd, n-digit pandigital numbers with even n cannot be prime. Additionally, for n=9 and n=8, the sum of digits 1-9=45 and 1-8=36 are divisible by 3, so those numbers are divisible by 3 and cannot be prime. Thus, the maximum possible is n=7, with sum 1+2+3+4+5+6+7=28, not divisible by 3.

Algorithm Analysis

The solution generates all permutations of digits “1,2,3,4,5,6,7”, converts each to a number, and checks if it’s prime. The largest prime found is the answer.

Key steps:

  • Generate all 7! = 5040 permutations
  • For each permutation, convert to integer and test primality
  • Track the maximum prime value found

Primality testing uses trial division up to √n.

Performance Analysis

  • Time Complexity: O(7! × √n) where n≈7.6×10⁶, resulting in ~5,000 × 3,000 = 15 million operations. Executes in milliseconds on modern hardware.
  • Space Complexity: O(1) excluding permutation generation space.
  • Execution Time: Very fast (<1 second), suitable for interactive applications.
  • Scalability: Factorial growth in permutations limits to small n; primality testing remains efficient.

Key Insights

  • The largest 7-digit pandigital prime is 7,652,413
  • Only n=7 produces pandigital primes due to parity and divisibility rules
  • Permutation generation is the bottleneck, but 7! is computationally feasible
  • Efficient primality testing is crucial for performance

Educational Value

This problem teaches:

  • Number theory concepts: pandigital numbers, primality, and digit sum properties
  • Combinatorial algorithms: generating permutations
  • Optimization: understanding constraints to reduce search space
  • Algorithm design: balancing brute force with mathematical insights
  • Programming techniques: permutation generation and primality testing