【Python】求素数-稍加优化

print 'Find prime number smaller then  input number \n'
print 'Please input a number:'
import datetime
begintime=datetime.datetime.now()
number=raw_input()
num=1
end=[]
b=0
n=0
while num<int(number):
    div=1
    while div<num:
        result=float(num)/div
        if float(result).is_integer():
            n+=1
            if n>1:break
        div+=1
    if n==1:
        end.append(num)
    num+=1
    n=0
print "The prime number smaller then %s are:\n" % str(number)
print end
endtime=datetime.datetime.now()
spendtime=endtime-begintime
print "The calculate spend %s " % spendtime
raw_input()

求10000以内素数,未优化前时长1分10秒,优化后时长11秒

你可能感兴趣的:(python)