SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Script Library
 Prime Number Table Function
 New Topic  Reply to Topic
 Printer Friendly
Previous Page
Author Previous Topic Topic Next Topic
Page: of 2

russell
Pyro-ma-ni-yak

USA
5072 Posts

Posted - 03/14/2013 :  11:23:36  Show Profile  Visit russell's Homepage  Reply with Quote
Nice. I'm impressed. I burned a few brain cells going through that
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 03/14/2013 :  18:09:28  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 03/14/2013 :  18:10:52  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Thank you.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
171 Posts

Posted - 06/02/2013 :  01:55:37  Show Profile  Reply with Quote
SwePeso, your code doesn't generate anything after 734869?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 06/02/2013 :  17:27:45  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 06/02/2013 :  17:28:38  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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"
Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
171 Posts

Posted - 06/03/2013 :  13:31:51  Show Profile  Reply with Quote
Impressive.
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Previous Page
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000