Sunday, March 08, 2009

Project Euler #41

After a while I went back to Project Euler to solve a problem. Boy those are addicting. Had some fun with the fairly simple #41

The idea was to find the largest n-digit pandigital that is also a prime. A n-digit number is pandigital if it makes use of all the digits 1 to n exactly once

Based on the simple isPrime rule I was able to rule out 8 digit and 9 digit pandigitals. This is because the sum of the numbers is evenly divisible by 9, sum(1..9) = 45 and sum(1..8) = 36. So this left me with the possibility that n <= 7 .

Here is the python code I used to find it:-


def isNPandigital(number):
return set( [int(c) for c in str(number)]) == set(range(1,len(str(number))+1))


t = time.time()
primes = sieve(7654321)
answerSet = []

print "primes generated"

for prime in primes:
if isNPandigital(prime):
answerSet.append(prime)

print max(answerSet)
print time.time() - t

No comments: