Monday, September 25, 2006

Return All 78498 Prime Numbers Between 1 and 1000000 In 3 seconds

That is right folks; SQL Server is capable of returning all 78498 prime numbers between 1 and 1000000 in 3 seconds. Who said that SQL Server isn't suitable for this task?

Let's start with a little bit of history; Ward Pond had a posting on his blog on how to create a table with 1000000 rows. Hugo Kornelis replied with a solution that ran in 1110 ms. For fun I left the following comment: “How about the next challenge is to return all 78498 prime numbers between 1 and 1000000?”

Ward took the challenge and posted a solution that would take hours to complete. Then Hugo Kornelis posted a solution that took 8 seconds. After that Ward tweaked Hugo’s solution and got it down to 3 seconds. That is just unbelievable. I wonder how long it would run if you were to code something like that in C, C++, C# or your favorite language?

Any takers?

1 comment:

Anonymous said...

I've jumped in...

http://robfarley.blogspot.com/2006/09/primes.html
http://robfarley.blogspot.com/2006/09/more-on-primes.html

You can tell me what you think. It's still a SQL-based solution.