-
Notifications
You must be signed in to change notification settings - Fork 0
/
question41.py
executable file
·53 lines (38 loc) · 1.32 KB
/
question41.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
46
47
48
49
50
51
#!/usr/bin/env python3
# Pandigital prime
#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?
# does not work. Check primality tests
# http://stackoverflow.com/questions/17298130/working-with-large-primes-in-python
def is_pandigital(num):
num_str = str(num)
numbers = list(range(1,len(num_str)+1))
for i in range(0,len(num_str)):
if int(num_str[i]) in numbers:
ind = numbers.index(int(num_str[i]))
del numbers[ind]
if len(numbers)==0: return 1
else: return 0
return 0
## sieve of eratosthenes
# edited to give list of primes with max 9 digits
# need a better version for the large numbers:
import numpy
def primes_upto2(limit):
is_prime = numpy.ones(limit + 1, dtype=numpy.bool)
for n in range(2, int(limit**0.5 + 1.5)):
if is_prime[n]:
is_prime[n*n::n] = 0
return numpy.nonzero(is_prime)[0][2:]
def find_largest(primes):
for p in reversed(primes):
if is_pandigital(p):
return p
return 0
# print(is_pandigital(12345))
# find primes max of 9 digits
temp1 = primes_upto2(1000000000)
#temp1 = [y for y in candidate_list if y>100000000]
largest = find_largest(temp1)
print(largest)