April 15, 2009 at 7:08 am
By the way, there are a lot of tweaks, tricks and improvements that can be applied to primes-listing algorithms, though only a few of them would be worth it for a number as low as 10,000. But, I have ones for upto 1,000,000 that are over a page long because of all the different tweaks & tricks (2005 only, sorry).
This is because the Sieve of Eratosthenes is a simple but inherently inefficient algorithm. Unfortunately, all of the progressivley faster algorithms are also progressively more complex.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
April 15, 2009 at 7:57 am
The version I posted has as its only virtue that it is simple and easy to understand. There are lots of ways to speed it up.
For example, you could add calculated columns that have the mod of dividing the number by the first ten primes, and index those columns. You can add something to the join where it only uses numbers that are less than half of the number you're testing (there's no point in trying to divide 500 by any number higher than 250, as it won't give a whole number in any case). You could play around with base data types (is float faster than int, or vice versa, for example) to see if you gain anything there.
Lots of ways to play around with it.
The whole point was that you don't have to use a loop/cursor for this.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Viewing 2 posts - 16 through 16 (of 16 total)
You must be logged in to reply to this topic. Login to reply