问题是:
素数3,7,109和673非常值得注意。通过取任何两个素数并以任何顺序连接它们,结果将是 永远是素数。例如,取7和109,7109和1097都是素数。这四个素数的总和,792,代表了 具有此属性的一组四个素数的最低总和。 找到一组五个素数的最低和,其中任何两个素数连接以产生另一个素数。
现在,以下是未在结果列表中提供任何输出的程序。请帮忙
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)
答案 0 :(得分: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
答案 1 :(得分: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 = []
答案 2 :(得分:0)
var jqxhr = $.post(url_update, { id: id , name: name});