SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Optimized prime number generator

By Goce Smilevski,

This a modification to the script given by Preethi.
It generates prime numbers to the upper bound you specify.
Modifications are using following facts from algebra:
1. All prime numbers greater than 3 can be written in the form 6 * X +/- 1.
2. Instead of checking the module for ALL numbers <= FLOOR(SQRT(@aX)), it is enough to check PRIME NUMBERS ONLY.
Also, the check is done using SQL on @Prime table, instead of a WHILE-loop.

Total article views: 264 | Views in the last 30 days: 1
Related Articles

Table Valued Function to return a range of prime numbers

A fast table valued function to generate a list of prime numbers within the range specified by the p...


Finding Primes

While it's not likely that many of you need to find prime numbers using T-SQL, it is an interesting ...


Creating Stored Procedure in SQL server 2000 for Printing Prime Numbers

"Trying to create a sp that prints Prime numbers upto 500"


Prime Number Table Generator (new ver crunches to 1 Mil in <10.5 sec!)

Generates prime numbers and puts them into a table for future reference by adhoc queries and databas...

sqlinsider scripts