-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path041.py
42 lines (32 loc) · 1.07 KB
/
041.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
33
34
35
36
37
38
39
40
41
42
"""
Project Euler Problem 41
========================
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?
"""
# Can only be 7 or 4 digits, and can't end with an even number
from itertools import permutations
from utils import is_prime
tuple_to_int = lambda tuple_: int("".join(map(str, tuple_)))
# first check 7 digits
exists_at_7_digits = False
for tuple_ in reversed(list(permutations(range(1, 8), 7))):
if tuple_[-1] % 2:
curr_num = tuple_to_int(tuple_)
if is_prime(curr_num):
largest = curr_num
exists_at_7_digits = True
break
if exists_at_7_digits:
print(largest)
else:
# have to go to the 4 digit case
for tuple_ in reversed(list(permutations(range(1, 5), 4))):
if tuple_[-1] % 2:
curr_num = tuple_to_int(tuple_)
if is_prime(curr_num):
largest = curr_num
break
print(largest)