Fastest Algorithm to calculate 10000 primes in python ?is it
import time st=time.time() mdu=0 check =3 noofprime=2 primeLs=[2,3] while noofprime!=10000: check=check+2 a=len(primeLs) b=primeLs[a-1] for div in primeLs: #print check mdu=check%div if mdu==0: break if div==b: noofprime= noofprime+1 primeLs=primeLs+[check] et=time.time() #print primeLs print st print et print et-st print primeLs
time python test.py 1296951327.14 1296951364.78 37.6408441067 104729 real 0m37.667s user 0m37.242s sys 0m0.321s
is it fastest in python as i could not find fastest time in google but found this on c++ http://www.troubleshooters.com/codecorn/primenumbers/primenumbers.htm below is the code avoid printing list it will take too much time but to verify i have put it there but commented and printed the last value ( 9999th value) verified from http://primes.utm.edu/lists/small/10000.txt
Mine isn't bad http://www.youtube.com/watch?v=p6fHllFsy7k
But it would have to be recoded a bit to stop after 1000 primes
time ./primgen.py 7179 # I said explicitly to stop at 7919 since it doesn't count primes real 0m1.302s user 0m0.356s sys 0m0.008s Not bad I suppose
Small point. You don't need to check for divisibility by 2 since you are generating only odd numbers to test. Big point. You can stop testing divisors when they exceed sqrt(x). At 7919, this is the difference between 999 test divisions (up to 7907) and 24 divisions (up to 83). You can also get a small speedup by generalizing the method of generating only odd numbers (not div by 2) to generating numbers not divisible by 2 or 3. Odd numbers not divisible by 3: 5,7, 11,13, 17,19, etc So instead of always counting by 2s, alternate counting by 2 and 4. This eliminates 1/3 of the numbers you are testing (and you could carry this on to 5,7, etc if it was worth doing, but diminishing returns hit fast, especially since the loops you skip would have been quick ones that failed on the first division tests).
On my computer, I got bored waiting for your program to finish generating 10k primes, but 5k primes took about 11 sec. Changing the loop to recognize numbers are prime when div>sqrt(x) took just 0.5 sec for 5000 primes, and about 1.4 sec for 10k primes.
@zifmia thanks for the tips i made changes as you suggested and it works, if you please verify the time as well posting the code below , will try for dict method as well and by the way do you think this code compromises space over time
import time from math import sqrt st=time.time() mdu=0 check =3 noofprime=2 primeLs=[2,3]#,5,7,11,13] prime=True while noofprime!=10000: check=check+2 if check%3==0:check=check+2 sqrcheck=int (sqrt(check))+1 a=len(primeLs) b=primeLs[a-1] for div in range (3,sqrcheck): prime=True mdu=check%div if mdu==0 : prime=False break if prime==True: noofprime= noofprime+1 primeLs=primeLs+[check] et=time.time() print primeLs[noofprime-1] print st print et print (et-st)/60
i get this time when checked without writing to list prime 104729 st:1298174950.11 et:1298174952.94 0.0471333344777
Join our real-time social learning platform and learn together with your friends!