SQL Prime编号功能

时间:2013-03-22 09:17:08

标签: sql sql-server sql-server-2012 primes

如果我有一个数字X并想用{-1}}使用sql-server说什么是最好的方法?

我只是导入一个素数表还是有一个算法对于较小的素数是相当有效的?

注意:我对大于约的数字不感兴趣。 1000万。

使用以下内容结束:

IsPrime(X) = true/false

7 个答案:

答案 0 :(得分:9)

如你所说,你可以有一张stores all the primes up to 10 million的表格。那么查看一个数字是否是素数是很简单的。那么问题是哪种方法会更快。我怀疑桌子会更快(我没有测试过这个说法)。

Prime Table Solution

SQL函数解决方案

解决方案0

以下是Finding prime numbers with a Transact-SQL function的一个解决方案:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
–- =============================================
–- Author:        Nicolas Verhaeghe
–- Create date: 12/14/2008
–- Description:   Determines if a given integer is a prime
/*

      SELECT dbo.IsPrime(1)

      SELECT dbo.IsPrime(9)

      SELECT dbo.IsPrime(7867)

*/
–- =============================================
CREATE FUNCTION [dbo].[isPrime]
(
      @NumberToTest int
)
RETURNS bit
AS
BEGIN
      -– Declare the return variable here
      DECLARE @IsPrime bit,
                  @Divider int

      –- To speed things up, we will only attempt dividing by odd numbers

      –- We first take care of all evens, except 2
      IF (@NumberToTest % 2 = 0 AND @NumberToTest > 2)
            SET @IsPrime = 0
      ELSE
            SET @IsPrime = 1 –- By default, declare the number a prime

      –- We then use a loop to attempt to disprove the number is a prime

      SET @Divider = 3 -– Start with the first odd superior to 1

      –- We loop up through the odds until the square root of the number to test
      –- or until we disprove the number is a prime
      WHILE (@Divider <= floor(sqrt(@NumberToTest))) AND (@IsPrime = 1)
      BEGIN

            –- Simply use a modulo
            IF @NumberToTest % @Divider = 0
                  SET @IsPrime = 0
            –- We only consider odds, therefore the step is 2
            SET @Divider = @Divider + 2
      END  

      –- Return the result of the function
      RETURN @IsPrime

END
解决方案1 ​​

这是通过how to find whether is a prime or non prime with one select statement?的另一个解决方案。其他评论中也有更多信息。

CREATE FUNCTION isPrime
(
    @number INT
)
RETURNS VARCHAR(10)
BEGIN
    DECLARE @prime_or_notPrime INT
    DECLARE @counter INT
    DECLARE @retVal VARCHAR(10)
    SET @retVal = 'FALSE'

    SET @prime_or_notPrime = 1
    SET @counter = 2

    WHILE (@counter <= @number/2 )
    BEGIN

        IF (( @number % @counter) = 0 )
        BEGIN
            set @prime_or_notPrime = 0
            BREAK
        END

        IF (@prime_or_notPrime = 1 )
        BEGIN
            SET @retVal = 'TRUE'
        END

        SET @counter = @counter + 1
    END
    return @retVal
END

答案 1 :(得分:6)

我怀疑这并没有发生在很多人身上,但你需要检查的是,每个新号码是否可以被以前的素数整除......

create table prime (primeno bigint)
declare @counter bigint
set @counter = 2
while @counter < 1000000
begin
if not exists(select top 1 primeno from prime where @counter % primeno = 0 )

- 上面,添加AND prime&lt; @counter / 2等将进一步减少检查开销。

insert into prime select @counter
set @counter = @counter + 1
end
select * from prime order by 1

懒惰的编码,但即使在像我旁边那样的慢速虚拟PC上,你也会在几分钟内拥有高达一百万的所有内容。除非我忽视了某些事情,否则我将其中的78,498个(如果你不算1)。

答案 2 :(得分:4)

CREATE  proc prime  
@no int  
as   
declare @counter int  
set @counter =2  
begin  
while(@counter)<@no  
 begin  
 if(@no%@counter=0)  
  begin  
  select 'Not prime'  
  return  
  end  
  set @counter=@counter+1  
 end  
 select 'prime'  
 return  
end  

--exec prime 10  

答案 3 :(得分:2)

基于sequence generation,有一种有趣的方法可以生成没有任何函数或迭代过程(while)的素数。基本上会生成2 .. @max序列,我们会选择current%other = 0序列中没有其他序列的所有数字:

declare @max INT = 10000

;WITH all_numbers(n) AS
(
    SELECT 2
    UNION ALL
    SELECT n+1 FROM all_numbers WHERE n < @max
)
select all1.n as prime
from all_numbers all1
where not exists (select 1 from all_numbers all2 where all2.n < all1.n AND all1.n % all2.n = 0)
order by all1.n
-- beware, 0 means no limit. Otherwise 32767 can be the max specified
OPTION (MAXRECURSION 0)

这个解决方案的主要缺点是性能(例如,在20000之前生成所有质数需要大约17秒。)

答案 4 :(得分:1)

如何(在PostgreSQL上测试)

SELECT Listagg (num, ',') 
         within GROUP (ORDER BY num) 
FROM   (SELECT n1.num   num, 
               SUM(CASE 
                     WHEN MOD(n1.num, n2.num) = 0 THEN 1 
                     ELSE 0 
                   END) AS cnt 
        FROM   (SELECT ROWNUM num 
                FROM   dual 
                CONNECT BY LEVEL <= 1000) n1, 
               (SELECT ROWNUM num 
                FROM   dual 
                CONNECT BY LEVEL <= 1000) n2 
        WHERE  n1.num <> 1 
               AND n2.num <> 1 
               AND n1.num >= n2.num 
        GROUP  BY n1.num) a 
WHERE  cnt = 1; 

答案 5 :(得分:0)

这是一个运行良好的简单脚本。根据需要调整@num:

function api() { ... }

答案 6 :(得分:-3)

Create PROC prime @num int , @flag int OUTPUT
AS
   Declare @i=50
   set

   while (@i<@num)
Begin
     if @i%@num=0
     break
set
     @i=@i+1
  End
  if @i=@num
 set
    @glag=1
else
   set
      @flag=2


Declare @ans int
Exec prime 50,@flag=@ans OUTPUT
if @ans=1
print 'The number is prime'
else
print 'The number is composite'