If I have a number X and want to say IsPrime(X) = true/false
using sql-server what is the best approach?
Do I just import a table of primes or is there an algorithm that is fairly efficient for the smaller primes?
Note: I'm not interested in numbers greater than approx. 10 million.
Ended up using the following:
CREATE FUNCTION [dbo].[isPrime]
(
@number INT
)
RETURNS VARCHAR(10)
BEGIN
DECLARE @retVal VARCHAR(10) = 'TRUE';
DECLARE @x INT = 1;
DECLARE @y INT = 0;
WHILE (@x <= @number )
BEGIN
IF (( @number % @x) = 0 )
BEGIN
SET @y = @y + 1;
END
IF (@y > 2 )
BEGIN
SET @retVal = 'FALSE'
BREAK
END
SET @x = @x + 1
END
RETURN @retVal
END