We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
DECLARE @i int = 0, @string varchar(4000)
WHILE @i < 1001
BEGIN
IF @i = 2 --Two is prime!
BEGIN
SELECT @string = CONVERT(varchar(10),@i)
SET @i += 1
CONTINUE
END
IF((@i % 2) = 0) --If not PRIME, then skip and continue on.
BEGIN
SET @i += 1
CONTINUE
END
IF @string IS NULL --If first time iterating, don't add &.
BEGIN
SELECT @string = CONVERT(varchar(10),@i)
SET @i += 1
CONTINUE
END
SELECT @string = @string + '&' + CONVERT(varchar(10),@i) --All other PRIME numbers.
SET @i += 1
END
PRINT @string;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Print Prime Numbers
You are viewing a single comment's thread. Return to all comments →
Why did this fail?
DECLARE @i int = 0, @string varchar(4000) WHILE @i < 1001 BEGIN IF @i = 2 --Two is prime! BEGIN
SELECT @string = CONVERT(varchar(10),@i) SET @i += 1 CONTINUE END IF((@i % 2) = 0) --If not PRIME, then skip and continue on. BEGIN
SET @i += 1 CONTINUE END IF @string IS NULL --If first time iterating, don't add &. BEGIN SELECT @string = CONVERT(varchar(10),@i) SET @i += 1 CONTINUE END SELECT @string = @string + '&' + CONVERT(varchar(10),@i) --All other PRIME numbers. SET @i += 1
END PRINT @string;