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: 262 | Views in the last 30 days: 3
 
Related Articles
SCRIPT
SCRIPT

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

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

Tags
miscellaneous    
sqlinsider scripts    
t-sql    
 
Contribute