-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler041.py
32 lines (24 loc) · 1.26 KB
/
Euler041.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Solution to Project Euler problem 41 by tdnzr.
# Approach: Generate pandigital numbers in reverse order,
# similar to what we did in problem 24 (lexicographic permutations),
# then stop once we've found one which is prime.
# Note: itertools returns items in lexicographic order. That's not alphabetical order!
# permutations of [1,0] yield the order (1, 0), (0, 1); [0,1] yields (0, 1), (1, 0).
# As I want to start with the numbers corresponding to the largest permutations,
# I've entered the numbers from largest to smallest in digitList.
from euler_toolbox import is_prime
from itertools import permutations
def run():
# By using a list of strings here, later on we don't have to convert from int to str as often:
digitList = ["9", "8", "7", "6", "5", "4", "3", "2", "1"]
for _ in range(9):
for pandigitalTuple in permutations(digitList):
numberString = ''.join(pandigitalTuple)
number = int(numberString)
if is_prime(number):
return number
# If no 9-digit pandigital prime has been found,
# remove the leftmost (largest) number from the list, then try again.
digitList.pop(0)
if __name__ == "__main__":
print(f"The largest n-digit pandigital prime that exists is: {run()}")