2014-02-19 3 views
0

Проблема заключается в том, как:Python Программа Нет Выходной

The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.

Теперь Ниже приводится программа, которая не дает никаких выходных данных в списке результатов. Пожалуйста, помогите

LIMIT = 20000 

prima = [] #empty list 

def Bsearch(lsta,low,high,search): #Binary search function to search a prime number 
    if low>high: 
     return 0 
    else: 
     mid = int((low+high)/2) 

     if search<lsta[mid]: 
      Bsearch(lsta,low,mid-1,search) 
     if search>lsta[mid]: 
      Bsearch(lsta,mid+1,high,search) 
     if search==lsta[mid]: 
      return 1 
    return 0 

def primes(LIMIT): #sieve to create prime numbers upto LIMIT 
    dic = {} #empty dictionary 
    for i in range(2,LIMIT): 
     dic[i] = 1 
    for i in range(2,LIMIT): 
     for j in range(i,LIMIT): 
      if i*j>LIMIT: 
       break 
      dic[i*j] = 0 
    for i in range(2,LIMIT): 
     if dic[i]==1: 
      prima.append(i) 

primes(LIMIT) 

result = [] 

for i in range(0,len(prima)): 
    print(str((i/len(prima)*100))+"% list passed") 

    tempa = [] 
    tempa.append(prima[i]) 
    count = 0 
    for j in range(i+1,len(prima)): 
     c1 = int(str(prima[j])+str(prima[i])) #first combination 
     c2 = int(str(prima[i])+str(prima[j])) #second combination 
     if(Bsearch(prima,0,len(prima)-1,c1) and Bsearch(prima,0,len(prima)-1,c2)): 
      print("small success : "+str(count)) 
      tempa.append(prima[j]) 
      count +=1 
     if(count==4): 
      result.append(tempa) 
      print("success!") 
      break 

for item in result: 
    print(item) 
+5

Если вы ожидаете, что показать что-то, вы можете захотеть 'печать (что-то)' где-то в вашем коде. –

+0

@WayneWerner print присутствует при последнем сообщении в строке 55 –

+0

Вы вообще ничего не получаете? Странно, я получаю 'IndexError: индекс списка вне диапазона'. Это ваш самый современный код? – Kevin

ответ

0
LIMIT = 20000 

prima = [] #empty list 

def Bsearch(lsta,low,high,search): #Binary search function to search a prime number 
    if low>high: 
     return 0 
    else: 
     mid = int((low+high)/2) 

     if search<lsta[mid]: 
      Bsearch(lsta,low,mid-1,search) 
     if search>lsta[mid]: 
      Bsearch(lsta,mid+1,high,search) 
     if search==lsta[mid]: 
      return 1 
    return 0 

def primes(LIMIT): #sieve to create prime numbers upto LIMIT 
    dic = {} #empty dictionary 
    for i in range(2,LIMIT): 
     dic[i] = 1 
    for i in range(2,LIMIT): 
     for j in range(i,LIMIT): 
      if i*j>LIMIT: 
       break 
      dic[i*j] = 0 
    for i in range(2,LIMIT): 
     if dic[i]==1: 
      prima.append(i) 

primes(LIMIT) 

result = [] 
1

Поскольку ваш поиск рекурсивен, вы должны возвращать результаты поиска.

def Bsearch(lsta,low,high,search): #Binary search function to search a prime number 
    retval = 0 
    if low>high: 
    retval = 0 
    else: 
    mid = int((low+high)/2) 

    if search == lsta[mid]: 
     # Make this test first as allows it to exit at once with success 
     retval = 1 
    elif search<lsta[mid]: 
     retval = Bsearch(lsta,low,mid-1,search) 
    else:  # search>lsta[mid] Since only 3 choices elif not needed 
     retval = Bsearch(lsta,mid+1,high,search) 
    return retval 
0
LIMIT = 20000 

prima = [] #empty list 

def Bsearch(lsta,low,high,search): #Binary search function to search a prime number 
    if low>high: 
     return 0 
    else: 
     mid = int((low+high)/2) 

     if search<lsta[mid]: 
      Bsearch(lsta,low,mid-1,search) 
     if search>lsta[mid]: 
      Bsearch(lsta,mid+1,high,search) 
     if search==lsta[mid]: 
      return 1 
    return 0 

def primes(LIMIT): #sieve to create prime numbers upto LIMIT 
    dic = {} #empty dictionary 
    for i in range(2,LIMIT): 
     dic[i] = 1 
    for i in range(2,LIMIT): 
     for j in range(i,LIMIT): 
      if i*j>LIMIT: 
       break 
      dic[i*j] = 0 
    for i in range(2,LIMIT): 
     if dic[i]==1: 
      prima.append(i) 

primes(LIMIT) 

result = [] 
Смежные вопросы