_php tutorial on implementation of prime number calculation in SQLSERVER2005

Source: Internet
Author: User
I will present a challenge, who can use Sqlseerver to propose the best method of calculating primes,
I used a new feature CTE and some TSQL implementations, but none of them are ideal, the former (CTE) has a limit, while the latter (TSQL) produces 1 million primes in 7 minutes
Can you do a better job?
Here are some of my Code passages
(TSQL Implementation)
SET NOCOUNT ON
Declare @prime table (prime int not null primary key)
--insert into @prime values (2)
--insert into @prime values (3)
--insert into @prime values (5)
--insert into @prime values (7)
--insert into @prime values (11)
declare @number int, @pc int
Set @number = 13
Set @pc = 1
While @pc < 1000000
Begin
If not exists (select 1 from @prime where @number% prime = 0 and Prime < sqrt (@number))
Begin
Insert INTO @prime Select @number
Set @pc = @pc 1
End
Set @number = @number
case if @number%2 = 1 then 2
When @number%3 = 2 Then 2
When @number%5 = 4 then 2
When @number%7 = 6 then 2
When @number = Ten then 2
Else 1 End
End
Select @pc
And
(CTE Implementation)
With SEQ
As (select number
UNION ALL
Select S.number
case if S.number%2 = 1 then 2
When s.number%3 = 2 Then 2
When S.number%5 = 4 then 2
When S.number%7 = 6 then 2
When s.number = Ten then 2
Else 1 End
From SEQ S
Where number < 32767
)
, Prime as (
Select S.number
From SEQ S
Where NOT EXISTS (select 1 from SEQ S2 where S2.number < S.number and (s.number)% S2.number = 0)
)
SELECT *
From prime
Option (Maxrecursion 32767)


http://www.bkjia.com/PHPjc/631230.html www.bkjia.com true http://www.bkjia.com/PHPjc/631230.html techarticle I will present a challenge, who can use Sqlseerver to propose the best method of calculating primes, I used a new feature CTE and some TSQL implementations, but none of them are ideal, the former (CTE) has limitations, and ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.