>>i = %d" %ifor j in range(2, i..._用phytho程序求素数">
当前位置:   article > 正文

求素数的python程序,Python-求素数程序

用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

<
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/593241
推荐阅读
相关标签
  

闽ICP备14008679号