最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

求素数的python程序,Python

互联网 admin 44浏览 0评论

求素数的python程序,Python

#!/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 "

求素数的python程序,Python

#!/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 "

发布评论

评论列表 (0)

  1. 暂无评论