>>i = %d" %ifor j in range(2, i..._用phytho程序求素数">
赞
踩
#!/usr/bin/python
#-*- coding: utf-8 -*-
import time
from math import sqrt
#根据概念判断:
def SelectPrime1(Num):
#2是素数
Prime = [2]
#循环计算3-n之间的数
for i in range(3,Num):
#print ">>>i = %d" %i
for j in range(2, i, 1):
#print ">j = %d" %j
if i % j == 0:
#print ">>j = %d" %j
break
else:
Prime.append(i)
else:
#print Prime
print ">>>The number of prime between 0 and %d is %d" %(Num,len(Prime))
#去掉偶数的判断,时间复杂度O(n/2)
def SelectPrime2(Num):
#2是素数
Prime = [2]
#循环计算3-n之间的数
for i in range(3,Num):
#print ">>>i = %d" %i
if i % 2 == 0:
continue
for j in range(3, i, 2):
#print ">j = %d" %j
if j != "" and (i % j == 0):
#print ">>j = %d" %j
break
else:
Prime.append(i)
else:
#print Prime
print ">>>The number of prime between 0 and %d is %d" %(Num,len(Prime))
def SelectPrime3(Num):
#2是素数
Prime = [2]
#循环计算3-n之间的数
for i in range(3,Num):
#print ">>>i = %d" %i
<Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。