A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
Fastest Algorithm to calculate 10000 primes in python ?is it
anonymous
 5 years ago
Fastest Algorithm to calculate 10000 primes in python ?is it

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0import time st=time.time() mdu=0 check =3 noofprime=2 primeLs=[2,3] while noofprime!=10000: check=check+2 a=len(primeLs) b=primeLs[a1] 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 etst print primeLs[9999]

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0time python test.py 1296951327.14 1296951364.78 37.6408441067 104729 real 0m37.667s user 0m37.242s sys 0m0.321s

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0is 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Mine isn't bad http://www.youtube.com/watch?v=p6fHllFsy7k

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0But it would have to be recoded a bit to stop after 1000 primes

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0time ./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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Small 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).

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0On 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0@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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0import 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[a1] 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[noofprime1] print st print et print (etst)/60

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0i get this time when checked without writing to list prime 104729 st:1298174950.11 et:1298174952.94 0.0471333344777
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.