-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path041.py
More file actions
43 lines (40 loc) · 1.12 KB
/
041.py
File metadata and controls
43 lines (40 loc) · 1.12 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
"""
Project Euler - 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?
"""
def isPandigital(strN):
Ln = [c for c in strN]
Ln.sort()
Lck=[str(i) for i in range(1,len(Ln)+1)]
if Ln == Lck:
return True
return False
# #########################################################################
def primes(n):
# Implementation of the prime finding algorithm of
# Sieve of Eratosthenes
num=n//2+n%2-1
pos=[True]*(num+1)
i_lim=int(n**0.5)>>1
for i in xrange(i_lim):
if not pos[i]: continue #position already marked as False, skipping
start=(i*(i+3)<<1)+3
step=(i<<1)+3
for j in xrange(start, num, step):
pos[j]=False
prime=[((i)<<1)+3 for i in xrange(num) if pos[i]]
return prime
# #########################################################################
# MAIN #
listPrimes=primes(7654321)
listPrimes.reverse()
maxPan=0
for p in listPrimes:
if isPandigital(str(p)):
print p
break
#output
#7652413