在SQL中,将所有素数打印到n之前最简单的方法是什么?

时间:2017-04-22 18:44:21

标签: sql sql-server algorithm primes

我应该使用什么样的算法来打印所有素数,直到让我们说1000?

首选SQL Server。

由于

6 个答案:

答案 0 :(得分:1)

您可以使用它来列出SQL

中小于1000的所有素数
;WITH temp AS
(
    SELECT 2 AS Value 
    UNION ALL
    SELECT t.Value+1 AS VAlue 
    FROM temp t
    WHERE t.Value < 1000
)
SELECT * 
FROM temp t
WHERE NOT EXISTS
            (   SELECT 1 FROM temp t2
                WHERE t.Value % t2.Value = 0 
                AND t.Value != t2. Value
            )
OPTION (MAXRECURSION 0)

演示链接:Rextester

答案 1 :(得分:0)

答案很简单:

假设您已经在表格中填充了数据

CREATE TABLE dbo.PrimeNumber(Num INT NOT NULL PRIMARY KEY);

您只需要一个简单的SELECT

SELECT * FROM dbo.PrimeNumber

答案 2 :(得分:0)

with prime
as
(
select 1 as 'start'
union all
select start+1 'start'
from prime where start<100
)

select e as prime_value  from
(select  a.start%b.start  as w, a.start as e from prime A  , Prime B
where --a.start% b.start<>0 and 
 b.start<a.start
--and a.start between 1 and 100
)A
where w=0
group by A.e 
having count(w) <=1



BY Nagaraj M-BE

答案 3 :(得分:0)

我为SQL Server编写的这段代码非常快,它将在12秒钟内找到所有小于1000万的素数:

DECLARE @Min int = 2, @Max int = 100000
--
IF OBJECT_ID('tempdb..#N','U') IS NOT NULL DROP TABLE #N
--
CREATE TABLE #N(N int NOT NULL, SqrtN int NOT NULL);
--
  WITH L0 AS (SELECT 'Anything' N FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) AS T(N)), -- 16 values
       L1 AS (SELECT A.N FROM L0 A, L0 B, L0 C, L0 D, L0 E, L0 F, L0 G, L0 H), -- 15^8  values (2562890625 more than enough for max value of int (2^31-1)
       L2 AS (SELECT TOP(@Max/6) CONVERT(int,6*ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) RowNum FROM L1)
INSERT INTO #N(N, SqrtN)
SELECT T.N, SQRT(N)
  FROM L2
 CROSS APPLY(VALUES(L2.RowNum-1),(L2.RowNum+1)) T(N)
 WHERE T.N BETWEEN @Min AND @Max
   AND 0 NOT IN (N%5,N%7,N%11,N%13,N%17,N%19,N%23,N%29,N%31,N%37,N%41,N%43,N%47,N%53,N%59)  -- Not interested in anything dividable by these low primes
--
ALTER TABLE #N ADD PRIMARY KEY CLUSTERED(N) WITH FILLFACTOR = 100
--
IF OBJECT_ID('tempdb..#Primes','U') IS NOT NULL DROP TABLE #Primes
--
SELECT Z.N Prime
  FROM (SELECT N FROM (VALUES(2),(3),(5),(7),(11),(13),(17),(19),(23),(29),(31),(37),(41),(43),(47),(53),(59)) T(N)
         WHERE T.N BETWEEN @Min AND @Max
         UNION ALL
        SELECT X.N
          FROM #N AS X
         WHERE NOT EXISTS(SELECT *
                            FROM #N AS C
                           WHERE C.N <= X.SqrtN 
                             AND 0 = X.N%C.N)) Z
 ORDER BY 1

答案 4 :(得分:0)

这是我的答案 我是在SQL Server中完成的

DECLARE @number INT = 4
DECLARE @isprime INT = 0
DECLARE @counter INT
DECLARE @result VARCHAR(MAX) = '2&3&'

WHILE (@number <= 1000)
BEGIN
    SET @counter = 2
    WHILE (@counter <= CAST(SQRT(@number) as INT))
    BEGIN
        IF (@number % @counter = 0)
        BEGIN
            SET @isprime = 0
            Break
        END
        ELSE
        BEGIN
            SET @isprime = 1
            SET @counter += 1
        END        
    END
    IF @isprime = 1
    SET @result += CAST(@number as VARCHAR(6)) + '&'
    SET @number += 1
END

PRINT(LEFT(@result, len(@result)-1))

这是输出

2&3&5&7&11&13&17&19&23&29&31&37 41&43&47&53&59&61&67&71&73&79&83&89&97&101 103 107&109 113 127&131 137 139&149 151 157 163&167 173 179&181&191 193 197&199 211 223&227 229 233&239 241 251&257&263&269&271&277&281&283&293&307&311&313&317&331&337&347&349&353&359&367&373&379&383&389&397&401&409&419&421&431&433&439 443 449&457&461&463&467&479&487&491&499&503&509&521&523&541&547&557&563&569&571&577 587&593&599&601&607&613&617&619&631&641&643&647&653 659&661&673&677&683&691&701&709&719&727&733&739&743&751&757&761&769&773&787&797&809&811&821&823&827&829&839&853&857&859&863&877&881&883&887&907&911&919&929&937&941&947&953&967&971&977&983&991&997

答案 5 :(得分:-1)

DECLARE @range int = 1000, @x INT = 2, @y INT = 2 

While (@y <= @range)
BEGIN
 while (@x <= @y) 
 begin
    IF ((@y%@x) =0) 
    BEGIN
        IF (@x = @y) 
            PRINT @y
            break
    END
 IF ((@y%@x)<>0)   
 set @x = @x+1
 end  
set @x = 2
set @y = @y+1 
end