ccyy's coding zone
往前走,不要留恋路边的风景.
posts - 25,comments - 9,trackbacks - 0
筛素数,在天哥的威逼利诱下改了筛素数的模板...以后PE都python了...
#Firename:pe37.py
import math
N=1000000
prime = [0 for row in range(N)]
res = []
top=0
def Prime():
    for i in range (2,N):
        prime[i]=1
    prime[0],prime[1]=0,0
    for m in range(2,N): 
        if prime[m]==1:
            prime[m]=1
            res.append(m)
            for k in range(2*m,N,m):
                prime[k]=0
tot=0
Prime()

for i in range(len(res)):
    temp=res[i]
    flag=0
    while temp>0:
        if prime[temp]!=1:
            flag=1
            break
        temp/=10
    temp=res[i]
    t=1
    while temp>0:
        temp/=10
        t*=10
    temp=res[i]
    while t>0:
        if prime[temp]!=1:
            flag=1
            break
        tmp=temp/t
        temp=temp-tmp*t
        t/=10
    if flag==0 and res[i]>10:
        top+=1
        tot+=res[i]
print tot
print top
后来发现python里面原来有直接可以求素数的函数  - -||
#prime_list(n) 返回一个list,存1 - n 内的素数
#prime_table(n) 返回一个list,用0 1 表示是不是素数        
            

posted on 2010-08-29 21:54 ccyy 阅读(137) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理