问题陈述 :
数字197被称为圆质数,因为数字的所有旋转:197,971和719本身都是质数.
100以下有13个这样的素数:2、3、5、7、11、13、17、31、37、71、73、79和97.
一百万以下有多少个圆形素数?
我的问题
我检查了所有代码,发现二进制搜索功能给出return 1语句作为输出打印成功.但是什么都没有添加到最终列表中.请帮忙
用Python编写程序:
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
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)
本文发布于:2024-01-28 18:28:32,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064377179391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |