-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |