-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem-041.py
45 lines (36 loc) · 1.19 KB
/
problem-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
43
44
45
### Problem 41 - Pandigital Prime
###---------------------------------------------------------------------------------------------------------
### 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?
### Solution
from itertools import permutations
# Function to determine if prime
def isPrime(n: int) -> bool:
if n < 2:
return False
elif n == 2:
return True
else:
i = 2
while i ** 2 <= n:
if n % i == 0:
return False
i += 1
return True
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
largest_num = 0
for a in range(1, len(digits)):
currDigits = digits[0:a]
numbers = list(permutations(currDigits))
nums = []
for i in numbers:
currNum = 0
for j in range(len(i)):
currNum += (10 ** (len(i) - (j + 1))) * i[j]
nums.append(currNum)
for i in nums:
if isPrime(i):
if i > largest_num:
largest_num = i
print("The largest pandigital prime is: " + str(largest_num))