SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

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

By Jesse Roberge, 2009/07/15

Total article views: 825 | Views in the last 30 days: 42

*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
Util_GeneratePrimes
Prime Generator
By Jesse Roberge - YeshuaAgapao@Yahoo.com

Generates prime numbers and puts them into a table for future reference by adhoc queries and database and application code.
Uses a table of numbers to enable computation with a set-based insert..select.
If you don't have a table of numbers, you can use one of my generators to set one up - http://www.sqlservercentral.com/Authors/Scripts/Jesse_Roberge/13538/
You may have to tweak this code to point to where you ultimately have/place your table of numbers.
a recursive-CTE fn_numbers is faster than a table of numbers only with <1000 numbers.
Utilizing a table numbers with 386,884 numbers, it generates 61,924 prime numbers (772,517 max) in 7.2 seconds on my Dell Inspiron M170 laptop (single-core 2.0GHZ centrino),
also generates Strong, Sophhie, and safe flags (1.5 additional seconds).
The Sophie flag only works on about first half of the prime numbers generated
if 60,000 priimes are generated, about 30,000th-60,000th primes will not have the sophie flag set even if they are sophie.

Required Input Parameters
none

Optional Input Parameters
@MinPrime=1 The minimum prime value to generate. You will usually want this at 1
@MaxPrime=2097152 The maximum prime value to generate.
The default value should use all of the values for most peoples' table of numbers
(622^2=386884 for mine; to completely fill 2-level deep clustered index).

Usage:
EXECUTE Admin.Util_GeneratePrimes @MinPrime=1, @MaxPrime=65536

Copyright:
Licensed under the L-GPL - a weak copyleft license - you are permitted to use this as a component of a proprietary database and call this from proprietary software.
Copyleft lets you do anything you want except plagarize, conceal the source, proprietarize modifications, or prohibit copying & re-distribution of this script/proc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

see <http://www.fsf.org/licensing/licenses/lgpl.html> for the license text.

*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

By Jesse Roberge, 2009/07/15

Total article views: 825 | Views in the last 30 days: 42
Your response
 
 
Related tags

Advanced Querying     SQL Server 2005    
Development     SQLInsider Scripts    
Miscellaneous     Table of Numbers    
nums     tally    
prime     T-SQL    
prime number     T-SQL Aids    
sieve    
 
 
Contribute

Free registration required...

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Login (existing users)

Login

Email:   Password:   Remember me: Forgotten your password?

Register (new users)

Register

Email:   Password:
Confirm:

Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

Steve Jones
Editor, SQLServerCentral.com