Skip to content

Commit

Permalink
Solves problem 41
Browse files Browse the repository at this point in the history
  • Loading branch information
Ippo343 committed Mar 6, 2017
1 parent 534d2ca commit 5c6d0d3
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions problem41.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
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?
"""

from utilities import divisors
from itertools import permutations


def largest_pandigital_prime(n):
digits = sorted(xrange(1, n + 1), reverse=True)
for comb in permutations(digits):

if comb[-1] in (0, 2, 4, 5, 6, 8):
# Even numbers and multiples of 5 are obviously not prime
continue

num = int(''.join(str(d) for d in comb))
if any(divisors(num, only_primes=True, only_proper=True)):
continue
else:
return num
else:
return None


if __name__ == '__main__':

# The solution can't have 5, 6, 8 or 9 digits because:
# sum(1..9) = 45
# sum(1..8) = 36
# sum(1..6) = 21
# sum(1..5) = 15
# So every pandigital number with that number of digits is divisible by 3
# The problem text tells you that 2143 is prime, therefore the solution must be at least 4 digits long
# and this rules out 3 too.
candidate_n = [7, 4]

for n in candidate_n:
p = largest_pandigital_prime(n)
if p:
print p
break

0 comments on commit 5c6d0d3

Please sign in to comment.