QUESTION: The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.코드에 논리적 오류가 있습니까?
Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
내 코드 출력 처음 다섯 개 같은 번호 만 수 있으며, 다음과 같은 오류를 보여줍니다
Traceback (most recent call last):
File "main.py", line 41, in
if check(i) and check(rev(i)):
File "main.py", line 30, in check
if sieve[n]:
IndexError: list index out of range
코드 :
sieve = [True] * 1000001 # Sieve is faster for 2M primes
def mark(sieve, x):
for i in xrange(x+x, len(sieve), x):
sieve[i] = False
sieve[0],sieve[1]=False,False
for x in xrange(2, int(len(sieve) ** 0.5) + 1):
if sieve[x]: mark(sieve, x)
def rev(n):
s=0
while n>0:
s=s*10+n%10
n/=10
return s
def check(n):
flag=1
while n>0:
if sieve[n]:
n/=10
else:
flag=0
break
return flag==1
ctr=0
i=11
s=0
while ctr!=11:
if check(i) and check(rev(i)):
print i
s+=i
ctr+=1
i+=1
print s
lww=raw_input()
아마도 체의 끝에서 도망 갔을까요? – user2357112
그럴 가능성이 있습니다. 11 번째 양방향으로 잘라낼 수있는 소수가 얼마나 큰지 아십니까? – jonrsharpe
예외를 잡아 내고'print n'을 잡을 코드를 추가하면 추측 할 필요없이 문제가된다면 실제로 알 수 있습니다 ... – abarnert