求大神指出我编的求循环素数个数python程序的错误
题目数字197可以被称为循环素数,因为197的三个数位循环移位后的数字:197,971,719均为素数。100以内这样的数字包括13个,2,3,5,7,11,13,17,...
题目 数字197可以被称为循环素数,因为197的三个数位循环移位后的数字:197,971,719均为素数。100以内这样的数字包括13个,2,3,5,7,11,13,17,31,37,71,73,79,97。要求任意正整数n以内一共有多少个这样的循环素数。
我的程序
import math
count=0
number=int(raw_input())
weishu=0
zhongjie=0
for s in range(2,number):
xianzaideshu=s
while xianzaideshu<>0:
xianzaideshu/=10
weishu+=1
#print weishu
yuanweishu=weishu
for chushu in range(2,int(math.sqrt(s))+1):
if s%chushu==0:
break
else :
while weishu<>0 :
s=s/10+(s%10)**weishu
weishu-=1
for chushu in range(2,int(math.sqrt(s))+1):
if s%chushu==0:
break
else:
zhongjie+=1
print s
if zhongjie==yuanweishu:
count+=1
zhongjie=0
if number>=2:
count+=1
print count 展开
我的程序
import math
count=0
number=int(raw_input())
weishu=0
zhongjie=0
for s in range(2,number):
xianzaideshu=s
while xianzaideshu<>0:
xianzaideshu/=10
weishu+=1
#print weishu
yuanweishu=weishu
for chushu in range(2,int(math.sqrt(s))+1):
if s%chushu==0:
break
else :
while weishu<>0 :
s=s/10+(s%10)**weishu
weishu-=1
for chushu in range(2,int(math.sqrt(s))+1):
if s%chushu==0:
break
else:
zhongjie+=1
print s
if zhongjie==yuanweishu:
count+=1
zhongjie=0
if number>=2:
count+=1
print count 展开
3个回答
2015-05-19
展开全部
#网上找到的资料,python2.7 可以运行以下代码。
#!/usr/bin/python
# encoding: utf-8
import time
import pprint
def allPrime(maxNum):
aList = range(0, maxNum)
def _do(aPrime):
for i in xrange(aPrime, maxNum, aPrime):
aList[i] = 0
return aPrime
return map(_do,(i for i in xrange(2,maxNum) if aList[i]))
t0 = time.time()
LIMIT = int(raw_input("请输入范围:"))
primelist = allPrime(LIMIT)
primesets = set(primelist)
def loopshift(num):
numlst = list(str(num))
result = list()
for i in range(len(numlst)):
x = int(''.join(numlst[i:] + numlst[:i]))
if x not in primesets:
return None
result.append(x)
result = sorted(set(result))
for x in set(result):
primesets.remove(x)
return result
found = list()
for num in primelist:
xn = loopshift(num)
if xn and xn not in found:
found.append(xn)
pprint.pprint(found)
print "Process usage", time.time()-t0
展开全部
自己写的, 希望可以帮到你
import math
def isPrime(y):
if y<=1:
return False
x = y // 2
while x > 1:
if y % x == 0:
return False
x -= 1
else: # Normal exit
return True
def isCirclePrime(y):
if y<=1:
return False
num_list = list(str(y))
for i in xrange(len(num_list)):
tmp = num_list[i:]+num_list[0:i]
num_tmp = int(''.join(tmp))
if not isPrime(num_tmp):
return False
else:
return True
if __name__=="__main__":
print "please input a number:"
num = input()
circle_prime_list=[]
for i in xrange(num):
if isCirclePrime(i):
circle_prime_list.append(i)
print circle_prime_list
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
s=s/10+(s%10)**weishu 这个算法不对
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询