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.
WITH NUM AS
(
SELECT 2 AS N
UNION ALL
SELECT N +1
FROM
NUM
WHERE
N < 999
) ,
primeNum AS
(
SELECT N
FROM
NUM A
WHERE
NOT EXISTS
(
SELECT 1
FROM
NUM B
WHERE
A.N % B.N = 0
AND A.N != B.N
)
)
SELECT
STRING_AGG(N,'&')
FROM
primeNum OPTION(MAXRECURSION 1000)
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 →
MS SQL Solution:
WITH NUM AS ( SELECT 2 AS N UNION ALL SELECT N +1 FROM NUM WHERE N < 999 ) , primeNum AS ( SELECT N FROM NUM A WHERE
NOT EXISTS ( SELECT 1 FROM NUM B WHERE A.N % B.N = 0 AND A.N != B.N ) ) SELECT STRING_AGG(N,'&') FROM primeNum OPTION(MAXRECURSION 1000)