Technical Article

Prime numbers generator

,

Very simple and straightforward script that creates a temporary table and fills it with prime numbers from 1 to the upper bound you specify. May be useful for educational purposes or even some cryptography applications.

CREATE TABLE #Prime
(PrimeN bigint)
GO

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

SET @aX = 1

WHILE @aX <= 1000 --Here is the upper bound
BEGIN
SET @Div = 0
SET @Temp = @aX / 2
SET @aY = 2
WHILE @aY <= @Temp
BEGIN
IF @aX % @aY = 0
BEGIN
SET @Div = 1
BREAK
END
SET @aY = @aY + 1
END

IF @Div = 0
BEGIN
INSERT INTO #Prime VALUES (@aX)
END
SET @aX = @aX + 1
END
GO

SELECT * FROM #Prime
DROP TABLE #Prime

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating