-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path041.py
More file actions
43 lines (34 loc) · 1023 Bytes
/
041.py
File metadata and controls
43 lines (34 loc) · 1023 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
35
36
37
38
39
40
41
42
43
"""
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?
Answer: d0a1bd6ab4229b2d0754be8923431404
"""
from common import check, is_prime
from itertools import permutations
PROBLEM_NUMBER = 41
ANSWER_HASH = "d0a1bd6ab4229b2d0754be8923431404"
# n must be a digit 1 - 9
# must also be prime
# could brute force?
def is_pandigital(number):
text = str(number)
n = len(text)
occurances = [0] * n
for i in range(n):
digit = int(text[i])
if digit > n:
return False
occurances[digit-1] += 1
if occurances[digit-1] > 1:
return False
return sum(occurances) == n
max_p = -1
for p in permutations("7654321"):
p = int("".join(p))
if is_prime(p):
check(p, PROBLEM_NUMBER, ANSWER_HASH)
exit()