Technical Article

Prime Numbers Generator (Updated)

,

This a modification to the script given by eugene_b.
It generates prime numbers to the upper bound you specify.
I have done some simple modifications to speed the process

Set NOCOUNT ON
DEclare @Prime Table
(PrimeN bigint NOT NULL PRIMARY KEY)

DECLARE
@aX bigint,
@aY bigint,
@Div bit,
@Temp bigint

SET @aX =  2 --2 is the least prime number

WHILE @aX <= 100000 --Here is the upper bound
BEGIN
SET @Div = 0
SET @Temp = FLOOR(SQRT(@aX)) 
SET @aY = 3
WHILE (@aY <= @Temp  AND @aY is NOT NULL)
    BEGIN
IF @aX % @aY = 0
BEGIN
     SET @Div = 1
BREAK
END
               SET @aY=@aY+2
END
IF @Div = 0 INSERT INTO @Prime VALUES (@aX)
IF (@aX=2) SET @aX=@aX+1 ELSE SET @aX = @aX + 2
END


SELECT * FROM @Prime

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating