Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: 257 | Views in the last 30 days: 2
 
Related Articles
ARTICLE

Finding Primes

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

FORUM

Creating Stored Procedure in SQL server 2000 for Printing Prime Numbers

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

SCRIPT

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...

SCRIPT

Prime numbers generator

Very simple and straightforward script that creates a temporary table and fills it with prime number...

FORUM

Performance challenge

Select prime numbers

Tags
miscellaneous    
sqlinsider scripts    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones