mirror of
https://github.com/correl/euler.git
synced 2024-11-23 19:19:53 +00:00
Problem 041
git-svn-id: file:///srv/svn/euler@74 e5f4c3ec-3c0c-11df-b522-21efaa4426b5
This commit is contained in:
parent
94bfd86b91
commit
e2e83ec3fe
1 changed files with 31 additions and 0 deletions
31
e041.py
Normal file
31
e041.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
"""What is the largest n-digit pandigital prime that exists?
|
||||
|
||||
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 e007 import is_prime
|
||||
from e035 import permutations
|
||||
|
||||
def pandigital_prime_generator(n):
|
||||
if n not in [3, 7]:
|
||||
raise Exception('Invalid pandigital prime length')
|
||||
for end in [3, 7, 9]:
|
||||
digits = range(1, n + 1)
|
||||
if end not in digits:
|
||||
continue
|
||||
digits.remove(end)
|
||||
for start in sorted(permutations(digits, len(digits)), reverse=True):
|
||||
number = int(''.join([str(i) for i in start] + [str(end)]))
|
||||
if is_prime(number):
|
||||
yield number
|
||||
|
||||
def main():
|
||||
for len in [9, 7, 3]:
|
||||
for n in sorted(pandigital_prime_generator(len), reverse=True):
|
||||
print 'Pandigital Prime:', n
|
||||
return
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in a new issue