-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem 41
More file actions
53 lines (42 loc) · 1.06 KB
/
Copy pathproblem 41
File metadata and controls
53 lines (42 loc) · 1.06 KB
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
46
47
48
49
50
51
52
53
"""
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?
"""
import math
from itertools import permutations
def all_primes(upper_bound):
primes = [2]
for i in range(3, upper_bound):
j = 0
while j < len(primes) and primes[j] <= int(i ** 0.5):
if i % primes[j] == 0:
break
j += 1
else:
primes.append(i)
return primes
def is_prime(x, primes):
sqrt = int(x ** 0.5) + 1
for prime in primes:
if x % prime == 0:
return False
if prime == primes[-1]:
return False
if prime > sqrt:
return True
return True
def is_prime(num, pri):
#for i in primes:
if num % 2 == 0:
return False
for i in range(3, math.ceil(math.sqrt(num))+ 1, 2):
if num % i == 0:
return False
return True
#primes = [2]
p = permutations("1234567")
for i in list(p)[::-1]:
x = "".join(i)
if is_prime(int(x)):
print(x)
break