MIT 6.00 Intro Computer Science (OCW) 64 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):

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

Latest Questions
2 minutes ago 0 Replies 0 Medals
JustKOI: a poem just becauuuse...
2 hours ago 5 Replies 10 Medals
madz2345: random stuff from my sketchbook. rate them :0
38 minutes ago 34 Replies 11 Medals
Fredyy: Do you all im doing better!!!pls sub it helps alot!!! thx
50 minutes ago 16 Replies 6 Medals
Tyrun: What y'all think of these lyrics. its sad rap
2 hours ago 10 Replies 2 Medals
lovelygirl13: i made a rick and morty collage
1 hour ago 24 Replies 9 Medals
DatBoiAYD: art
15 hours ago 14 Replies 6 Medals