- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem41.py
More file actions
Latest commit
43 lines (33 loc) · 1022 Bytes
/
Copy pathProblem41.py
File metadata and controls
43 lines (33 loc) · 1022 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
"""
@author: mc-es
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: 7652413
"""
fromitertoolsimportpermutations
importmath
defisPrime(n):
ifn<=1:
returnFalse
ifn==2:
returnTrue
ifn%2==0:
returnFalse
foriinrange(3, int(math.sqrt(n)) +1, 2):
ifn%i==0:
returnFalse
returnTrue
deflargestPandigitalPrime():
forninrange(9, 0, -1):
digits="".join(str(i) foriinrange(1, n+1))
pandigitalNumbers=sorted(
["".join(p) forpinpermutations(digits)], reverse=True
)
fornumStrinpandigitalNumbers:
num=int(numStr)
ifisPrime(num):
returnnum
returnNone
print(largestPandigitalPrime())