2014-03-29 4 views
3

문제 설명 : 197, 971, 719, 총리 자신이다 : 모든 숫자 회전이 때문에 수, 197, 원형 소수라고순환 소수 잘못된 출력 파이썬 프로그램

. 이하 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 및

97 얼마나 많은 순환 소수 거기 :

100 이하 열세 등 소수있다 1 백만?

내 문제 모든 코드를 검사 한 결과 이진 검색 기능이 출력 인쇄 성공으로 return 1 문을 제공한다는 것을 알았습니다. 그러나 최종 목록에는 아무 것도 추가되지 않습니다. 파이썬에서

프로그램 도와주세요 :

from time import time 
start = time() 
LIMIT = 1000000 # largest limit of the prime numbers 
prima = [] # list of primes later to be filled by primes function 

# binary search function 
def Bsearch(lsta,low,high,search): 
     if low>high: 
       return -1 
     else: 
      mid = int((low+high)/2) 
      if search<lsta[mid]: 
       Bsearch(lsta,low,mid-1,search) 
      elif search>lsta[mid]: 
       Bsearch(lsta,mid+1,high,search) 
      elif search==lsta[mid]: 
       print("Success!") 
       return 1 

# prime number generating function 
# uses sieve of Era** algorithm 
# produces correct result tested 
def primes(LIMIT): 
    lsta = {} # temporaty empty dictionary 
    for i in range(2,LIMIT): 
     lsta[i] = 1 
    for i in range(2,LIMIT): 
     for j in range(i,LIMIT): 
      if i*j>LIMIT: 
       break 
      lsta[i*j] = 0 
    for i in range(2,LIMIT): 
     if(lsta[i]==1): 
      prima.append(i) 
primes(LIMIT) 

final = [] 
for item in prima: 
    x = int(str(item)[::-1]) 
    # real problem here the following statement not inserting any value in final list 
    if(Bsearch(prima,0,len(prima)-1,x)): 
     print("Hello!") 
     print(final) 
     final.append(item) 
print(final) 
+4

재귀 호출에 return 문이 없습니다. 또한 -1은 참값입니다. True 또는 False를 반환합니다. – geoffspear

+0

@Wooble 오 세상에! .. 고마워 .. 그런 어리석은 실수에 너무 많은 시간을 낭비했다. –

+0

우린 응답으로 게시해야하는데, 지금은이 질문에 아직 답변이 없다. –

답변

0

를 신속하게이 목록 순환 소수를위한 가장 빠른 알고리즘 prime numberslist out Circular Prime numbers

def primes(max_n): 
    numbers = range(3, max_n+1, 2) 
    half = (max_n)//2 
    initial = 4 
    for step in xrange(3, max_n+1, 2): 
     for i in xrange(initial, half, step): 
      numbers[i-1] = 0 
     initial += 2*(step+1) 

     if initial > half: 
      return [2] + filter(None, numbers) 


def rotate(S_list): 
    S=[] 
    for i in range(len(S_list)): 
     S.append(int(S_list[i:]+S_list[:i])) 
    return set(S) 

def circularPrime(limit): 
    All_primes_in_limit = primes(limit) 
    circular_prime=[] 
    reject_list=['0','2','4','5','6','8'] 
    All_primes_in_limit=[i for i in All_primes_in_limit if not any(j in reject_list for j in set(str(i)))] 
    while All_primes_in_limit: 
     ShufleSet=rotate(str(All_primes_in_limit[-1])) 
     PrimesSet=set(All_primes_in_limit) 
     if not ShufleSet - PrimesSet: 
      circular_prime+=list(ShufleSet) 
     All_primes_in_limit=list(PrimesSet-ShufleSet) 
    circular_prime.sort() 
    return circular_prime 


#for limit value 1000000 
print circularPrime(1000000) 

을 생성