# 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? # Digit sum trick! number is divisible by 3 if and only if the digit sum of the number is divisible by 3 # simplifies problem to either 4 digit or 7 digit prime # generate all primes beneath 7654321 # generate all possible 7 digit pandigital combinations # largest 4 digit prime is 4231 from eulerhelpers import genprimes, isPandigital from itertools import permutations if __name__ == "__main__": primes = genprimes(7654321) x = 7654321 while True: if isPandigital(x) and x in primes: print x break x -= 1 if x <= 1000000: break
from eulerhelpers import isPandigital #not as simple as saying 987654321 because n has to be greater than 1 #at n=2, it has to be 5 digit and 4 digit number #seed must be 5000 or below largest_pan = 123456789 for seed in range(9,10000): mult = 1 testpan = '' while len(testpan) < 9: testpan += str(seed*mult) if len(testpan) >= 9: if isPandigital(int(testpan)) and int(testpan) > largest_pan: largest_pan = int(testpan) mult+=1 print "largest pandigital is", largest_pan