Project Euler 41 - Pandigital prime

Official link: https://projecteuler.net/problem=41

Thought Process

I initially solved this problem using itertools.permutations by simply generating all the permutations and checking if they are an n-digit pandigital, but this feels a bit like cheating so I thought of a smarter way to solve this problem.

  1. a 9 digital pandigital has the digit sum 1+2+3+4+5+6+7+8+9 = 45 which is divisible by 3, and we know that if the sum of a numbers digit's is divisible by 3 then the number itself is divisible by 3, which means it can never be prime

  2. a 8 digital pandigital has the digit sum 1+2+3+4+5+6+7+8 = 36, same as above this is always divisible by 3

  3. The following is not true for 7 digit pandigital's

  4. a 6 digital pandigital has the digit sum 1+2+3+4+5+6 = 21, same as above this is always divisible by 3

  5. a 5 digital pandigital has the digit sum 1+2+3+4+5 = 15, same as above this is always divisible by 3

  6. The following is not true for 4 digit pandigital's

  7. a 3 digital pandigital has the digit sum 1+2+3 = 6, same as above this is always divisible by 3

  8. a 2 digital pandigital has the digit sum 1+2 = 3, same as above this is always divisible by 3

  9. the only 1 pandigital number is 1, which is not prime

Therefore we conclude that only 7 digit and 4 digit primes can be n-digit pandigital, I generate these primes with my prime generation function

Interactive Code

Input an integer (yourinput)

Code will output the maximum pandigital < yourinput