Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ
 All Forums  General SQL Server Forums  Script Library  Prime Number Table Function Reply to Topic  Printer Friendly
Author  Topic
Page: of 2

russell
Pyro-ma-ni-yak

USA
5072 Posts

 Posted - 03/14/2013 :  11:23:36 Nice. I'm impressed. I burned a few brain cells going through that

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 03/14/2013 :  18:09:28 This runs in about 1.5 seconds to get all primes below 1,000,000```CREATE TABLE #Numbers ( Prime INT NOT NULL, Number BIGINT PRIMARY KEY CLUSTERED ); DECLARE @Max INT = 1000000; WITH n0(p) AS ( SELECT 1 UNION ALL SELECT 1 ), n1(p) AS ( SELECT 1 FROM n0 AS a CROSS JOIN n0 AS b ),n2(p) AS ( SELECT 1 FROM n1 AS a CROSS JOIN n1 AS b ),n3(p) AS ( SELECT 1 FROM n2 AS a CROSS JOIN n2 AS b ),n4(p) AS ( SELECT 1 FROM n3 AS a CROSS JOIN n3 AS b ),n5(p) AS ( SELECT 1 FROM n4 AS a CROSS JOIN n4 AS b ) INSERT #Numbers ( Prime, Number ) SELECT f.Prime, f.Prime * f.Prime AS Number FROM ( SELECT TOP (1 + @Max / 30) 30 * ROW_NUMBER() OVER (ORDER BY p) FROM n5 ) AS v(Value) CROSS APPLY ( VALUES (v.Value - 23), (v.Value - 19), (v.Value - 17), (v.Value - 13), (v.Value - 11), (v.Value - 7), (v.Value - 1), (v.Value + 1) ) AS f(Prime) WHERE f.Prime <= @Max; SELECT Prime FROM ( VALUES (2), (3), (5) ) AS v(Prime) WHERE Prime <= @Max UNION ALL SELECT n.Prime FROM #Numbers AS n WHERE NOT EXISTS ( SELECT * FROM #Numbers AS p WHERE p.Number <= n.Prime AND n.Prime % p.Prime = 0 ) DROP TABLE #Numbers;```N 56°04'39.26"E 12°55'05.63" Edited by - SwePeso on 03/14/2013 18:23:54

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 03/14/2013 :  18:10:52 Thank you.N 56°04'39.26"E 12°55'05.63"

Rasta Pickles
Posting Yak Master

United Kingdom
174 Posts

 Posted - 06/02/2013 :  01:55:37 SwePeso, your code doesn't generate anything after 734869?

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 06/02/2013 :  17:27:45 The code is set based, which means there is no guaranteed ordering.Add an ORDER BY to the output.N 56°04'39.26"E 12°55'05.63"

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 06/02/2013 :  17:28:38 ```CREATE TABLE #Numbers ( Prime INT NOT NULL, Number BIGINT PRIMARY KEY CLUSTERED ); DECLARE @Max INT = 1000000; WITH n0(p) AS ( SELECT 1 UNION ALL SELECT 1 ), n1(p) AS ( SELECT 1 FROM n0 AS a CROSS JOIN n0 AS b ),n2(p) AS ( SELECT 1 FROM n1 AS a CROSS JOIN n1 AS b ),n3(p) AS ( SELECT 1 FROM n2 AS a CROSS JOIN n2 AS b ),n4(p) AS ( SELECT 1 FROM n3 AS a CROSS JOIN n3 AS b ),n5(p) AS ( SELECT 1 FROM n4 AS a CROSS JOIN n4 AS b ) INSERT #Numbers ( Prime, Number ) SELECT f.Prime, f.Prime * f.Prime AS Number FROM ( SELECT TOP (1 + @Max / 30) 30 * ROW_NUMBER() OVER (ORDER BY p) FROM n5 ) AS v(Value) CROSS APPLY ( VALUES (v.Value - 23), (v.Value - 19), (v.Value - 17), (v.Value - 13), (v.Value - 11), (v.Value - 7), (v.Value - 1), (v.Value + 1) ) AS f(Prime) WHERE f.Prime <= @Max; WITH ctePrimes(Prime) AS ( SELECT Prime FROM ( VALUES (2), (3), (5) ) AS v(Prime) WHERE Prime <= @Max UNION ALL SELECT n.Prime FROM #Numbers AS n WHERE NOT EXISTS ( SELECT * FROM #Numbers AS p WHERE p.Number <= n.Prime AND n.Prime % p.Prime = 0 ) ) SELECT Prime FROM ctePrimes ORDER BY Prime; DROP TABLE #Numbers;```N 56°04'39.26"E 12°55'05.63"

Rasta Pickles
Posting Yak Master

United Kingdom
174 Posts

 Posted - 06/03/2013 :  13:31:51 Impressive.
Page: of 2  Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC