DECLARE @c int = 1000;
DECLARE @numbers  TABLE (n int NOT NULL PRIMARY KEY);
DECLARE @products TABLE (p int NOT NULL PRIMARY KEY);
DECLARE @primes   TABLE (p int NOT NULL PRIMARY KEY);

-- The 'composite exclusion' approach

-- 1. list all n = 2, 3, 4, ... c
WITH numbers AS
(
    SELECT  2 AS n
    UNION ALL
    SELECT n + 1 FROM numbers
    WHERE   n <= @c - 1
)
INSERT INTO @numbers SELECT n FROM numbers OPTION(MAXRECURSION 0);

-- 2. find all products n x n <= c
WITH products AS
(
    SELECT  DISTINCT m.n * n.n AS p
    FROM    @numbers m LEFT OUTER JOIN
            @numbers n ON 1 = 1
    WHERE   m.n * n.n <= @c
)
INSERT INTO @products SELECT p FROM products;

-- 3. numbers with no matching products are not composite, i.e, they're prime numbers.
INSERT INTO @primes
SELECT n.n FROM @numbers n LEFT JOIN @products p ON n.n = p.p WHERE p.p IS NULL;

这有点像埃拉托舍内斯方法的一次过筛。
我见过循环、存储过程等,以及伪代码和其他语言实现,但在我看来,这种源于素数定义的简单、基于集合的方法应该足够了。
请记住,我现在不关心性能、内存消耗或优化,我也没有用更大的数字来测试它。我只想发布算法,让人们确认(或质疑)从列表中排除复合数字就足够了。

最佳答案

递归cte(rcte)很少是性能最好的解决方案。下面是一个使用计数表的方法,它是hugo kornelis在这里发布的方法的一个稍微修改过的版本:https://sqlserverfast.com/blog/hugo/2006/09/the-prime-number-challenge-great-waste-of-time/
让我们将计数表解与rcte解进行比较:

SET STATISTICS TIME ON;

PRINT 'tally table approach'+char(13)+char(10)+replicate('-',50);
DECLARE @primes   TABLE (p int NOT NULL PRIMARY KEY);
DECLARE @limit bigint = 10000;

WITH E(x) AS (SELECT * FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) t(x)),
iTally(N) AS (SELECT TOP(@limit) ROW_NUMBER() OVER (ORDER BY (SELECT 1)) FROM E a, E b, E c, E d, E f)
INSERT @primes
SELECT      n1.N
FROM        itally AS n1
WHERE       n1.N > 1
AND         n1.N < @Limit
AND NOT EXISTS
 (SELECT    *
  FROM      itally AS n2
  WHERE     n2.N < @limit
  AND       n2.N BETWEEN 2 AND n1.N-1
  AND       n1.n % n2.N = 0)
--ORDER BY N
GO

PRINT 'rCTE approach'+char(13)+char(10)+replicate('-',50);
DECLARE @c int = 10000;
DECLARE @numbers  TABLE (n int NOT NULL PRIMARY KEY);
DECLARE @products TABLE (p int NOT NULL PRIMARY KEY);
DECLARE @primes   TABLE (p int NOT NULL PRIMARY KEY);

WITH numbers AS
(
    SELECT  2 AS n
    UNION ALL
    SELECT n + 1 FROM numbers
    WHERE   n <= @c - 1
)
INSERT INTO @numbers SELECT n FROM numbers OPTION(MAXRECURSION 0);

-- 2. find all products n x n <= c
WITH products AS
(
    SELECT  DISTINCT m.n * n.n AS p
    FROM    @numbers m LEFT OUTER JOIN
            @numbers n ON 1 = 1
    WHERE   m.n * n.n <= @c
)
INSERT INTO @products SELECT p FROM products;

-- 3. numbers with no matching products are not composite, i.e, they're prime numbers.
INSERT INTO @primes
SELECT n.n FROM @numbers n LEFT JOIN @products p ON n.n = p.p WHERE p.p IS NULL;

SET STATISTICS TIME OFF;

结果是:
tally table approach
--------------------------------------------------

 SQL Server Execution Times:
   CPU time = 3042 ms,  elapsed time = 3241 ms.
SQL Server parse and compile time:
   CPU time = 0 ms, elapsed time = 10 ms.

rCTE approach
--------------------------------------------------

 SQL Server Execution Times:
   CPU time = 14976 ms,  elapsed time = 15757 ms.

如您所见,针对10000的计数表方法速度快了5倍,而且也不会产生任何读操作(rCTE产生一吨!)
如果你真的在使用素数,最快的方法就是把它们存储在一个表中,这样你就不必每次需要素数时都计算它们了。

关于sql - 这是列出质数的好算法吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40642383/

10-10 17:33