-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path041.py
More file actions
34 lines (26 loc) · 892 Bytes
/
041.py
File metadata and controls
34 lines (26 loc) · 892 Bytes
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
"""
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?
"""
# THOUGHTS:
#
# There is no 9-digit pandigital prime, because 1+2+3+..+9 = 45 and 45%3 = 0,
# so all the 9-digit pandigitals are also divisible by 3
# 1+2+..+8 = 36 and 36 % 3 = 0
# 1+2+..+7
from itertools import permutations
from util import PrimeFactory
def main():
factory = PrimeFactory()
for num_digits in range(7, 1, -1):
for num_list in permutations(range(num_digits, 0, -1)):
num = int(''.join(map(str, num_list)))
if factory.is_prime(num):
return num
return 'No pandigital prime found??'
if __name__ == "__main__":
print(main())