Cool program!
The inner for loop can test up to math.sqrt(counter) (== counter**(0.5)), because any factor f of counter has to be paired with counter/f, and either f or counter/f is guaranteed to be <= sqrt(counter). That can save you some time.
Also, the Sieve of Eristothanes is worth checking out both for math's sake and also for faster code's sake.
Regards,
Jeff
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
I think your print to the display will be by far the slowest operation.
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
yea, ditch the printing and append it to a list and it'll be almost instant. :)
Even with ditching print and returning list of primes, this algorithm is not like speed demon. It takes 6,000ms for primes up to 10,000 compared to 4ms with standard sieve algorithm.
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
But wouldn't you guys be impressed if I, a novice at python, were able of making a python script as fast or faster than the sieve thingy?
Admit it, you would all be amazed.
Indeed we would. And the fellows from Fort Meade (NSA) would come knocking at your door, too. Something like a guaranteed job ... :)
Seriously, the program is good; the sieve recommendation is just for "further study."
Jeff
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
With from __future__ import division I can see a problem in line for i in range(2,(i2/2)): you get a float from the division and range does not like floats. Change this line to for i in range(2,(i2//2)):
Nice thinking here, have to see what Bumsfeld's timing comes up with.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
When you use 'from __future__ import division' then the '/' operator will do floating point division. Python actually has a '//' operator for integer division (floor or quotient) that was seldomly used, since '/' did the same thing. With new versions of Python the '//' will become more popular, since integer divisions are still needed.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
With new versions of Python the '//' will become more popular, since integer divisions are still needed.
... and old code will break frequently, since division will no longer yield integers. :eek:
Jeff
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
I sped it up again, "if I'm not even bothering to check even numbers, why divide by 2,4,6,etc.?"
replace:
for i in range(2,(i2//2)):
With
for i in range(3,(i2//2),2):
Great thinking! I tested it out with 10,000 primes. That made it about 4 times faster than the previous code.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
... and old code will break frequently, since division will no longer yield integers. :eek:
Jeff
This is why it hasn't been implemented yet in a large number of other languages like C, C++ either.
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213