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