Fastest Algorithm to calculate 10000 primes in python ?is it
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
for div in primeLs:
time python test.py
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
Not the answer you are looking for? Search for more explanations.
Mine isn't bad
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
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
from math import sqrt
for div in range (3,sqrcheck):
if mdu==0 :
i get this time when checked without writing to list