hab mal fix was gecodet, vielleicht brauch der eine oder andere das Programm mal.
Hier kann man auch gut sehen, das xrange durchaus schneller als range ist. Desweiteren gibt es noch die funktion pass oder break, wobei break weitaus schneller ist, da es schon abbricht wenn die Zahl durch irgendeine Teilbar ist, bei pass wird das einfach ignoriert!
Viel Spaß!
Code: Alles auswählen
from time import time
from math import sqrt
def check_if_prime_old(a):
if a%2==0 and a!=2:
return 0
else:
for b in range(3, int(sqrt(a))+1, 2):
if a%b==0:
return 0
return 1
def check_if_prime_new(a):
if a%23!=0:
for b in xrange(27, int(sqrt(a))+1, 2):
if a%b == 0:
return 0
return 1
else: return 0
def start_old(ranges):
result=[]
times=[time(), 0, 0]
for i in range(3, ranges-1):
if check_if_prime_old(i)==1:
result.append(i)
times[1]=time()
times[2]=times[1]-times[0]
return [times[2], [2]+result, len([2]+result)]
def start_new(ranges):
result=[]
times=[time(), 0, 0]
for i in xrange(5, ranges-1, 2):
if i%3!=0 and i%5!=0 and i%7!=0 and i%11!=0 and i%13!=0 and i%17!=0 and i%19!=0:
if check_if_prime_new(i)==1:
result.append(i)
times[1]=time()
times[2]=times[1]-times[0]
result=[2, 3, 5, 7, 11, 13, 17, 19, 23]+result
return [times[2], result, len(result)]
ranges=2
result=[[0, 0, 0], [0, 0, 0]]
for i in range(ranges):
a=start_new(200000)
b=start_old(200000)
result[0][0]=result[0][0]+a[0]
result[1][0]=result[1][0]+b[0]
result[0][1]=a[1]
result[1][1]=b[1]
result[0][2]=a[2]
result[1][2]=b[2]
result[0][0]=result[0][0]/ranges
result[1][0]=result[1][0]/ranges
print result[0][0]-result[1][0]
print result[0][0], result[1][0]
print result[0][2], result[1][2]
Edit2// Neuer Code! Vieeeel schneller!