DECLARE @i INT, @a INT, @count INTSET @i = 1WHILE (@i <= 500) BEGIN SET @count = 0 SET @a = 1 WHILE (@a <= @i) BEGIN IF (@i % @a = 0) SET @count = @count + 1 SET @a = @a + 1 END IF (@count = 2) PRINT @i SET @i = @i + 1 END
ha ha !! That laugh was for your(@GSquared) assumption being true. Yes, it is a home work. But I did tried my best before seeking help and was looped in between 'BEGIN' and 'END' since I am new to the SQL platform.
DECLARE@i INT,@a INT,@count INTSET @i = 1WHILE (@i <= 500)BEGIN SET @count = 0 SET @a = 1 WHILE (@a <= @i) BEGIN IF (@i % @a = 0) SET @count = @count + 1 SET @a = @a + 1 END IF (@count = 2) PRINT @i SET @i = @i + case when @i < 3 then 1 else 2 endEND
DECLARE @Max INT, -- Max number for prime search @i INT, -- Counter for possible candidates @count INT , -- row count of matches @TimeStart DateTime , -- Time execution startSET NOCOUNT ON-- Create Temp Table for storing resultsCreate Table #Primes(PrimeNumber Int Not Null)-- Initialize variablesSET @TimeStart = GetDate()SET @Max = 10000SET @count = 0SET @i = 2 WHILE (@i <= @Max) BEGIN SET @count = (SELECT COUNT(*) FROM #Primes WHERE (@i % PrimeNumber) = 0 ) If @count = 0 INSERT INTO #Primes(PrimeNumber) Values(@i) SET @i = @i + 1 END-- Output the resultsSELECT * FROM #primes-- output execution in secondsprint DateDiff(ss, GetDate(), @TimeEnd)DROP TABLE #Primes
set nocount on;create table #Numbers (Number smallint identity primary key);goinsert into #Numbers default values;go 500select N1.Numberfrom #Numbers N1inner join #Numbers N2 on N1.Number%N2.Number = 0group by N1.Numberhaving count(*) <= 2order by N1.Number;