Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 31 Online
OpenStudy (anonymous):

Fastest Algorithm to calculate 10000 primes in python ?is it

OpenStudy (anonymous):

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[9999]

OpenStudy (anonymous):

time python test.py 1296951327.14 1296951364.78 37.6408441067 104729 real 0m37.667s user 0m37.242s sys 0m0.321s

OpenStudy (anonymous):

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

OpenStudy (anonymous):

Mine isn't bad http://www.youtube.com/watch?v=p6fHllFsy7k

OpenStudy (anonymous):

But it would have to be recoded a bit to stop after 1000 primes

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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).

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

@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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

i get this time when checked without writing to list prime 104729 st:1298174950.11 et:1298174952.94 0.0471333344777

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
Lilmunchin: Trump or Biden
8 minutes ago 15 Replies 1 Medal
ARTSMART: Art!
45 minutes ago 5 Replies 4 Medals
Jasonisyours: What were the key causes of the French Revolution in 1789?
39 minutes ago 3 Replies 5 Medals
PureSoulless: Why is the word "Pedophile" always censored in yt vids?
1 day ago 3 Replies 0 Medals
Jalli: What's 58x3634u00b07
22 hours ago 6 Replies 3 Medals
arriya: who wanna play roblox
1 day ago 5 Replies 1 Medal
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!