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 Numbers AS (
SELECT 2 AS num
UNION ALL
SELECT num + 1
FROM Numbers
WHERE num < 1000
),
nonprime AS (
SELECT N1.num
FROM Numbers N1
JOIN Numbers N2
ON N2.num < N1.num AND N1.num % N2.num = 0
)
SELECT STRING_AGG(CAST(num AS VARCHAR), '&') AS concatenated_value
FROM Numbers
WHERE num NOT IN (SELECT num FROM nonprime)
OPTION (MAXRECURSION 0);
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 →
WITH Numbers AS ( SELECT 2 AS num UNION ALL SELECT num + 1 FROM Numbers WHERE num < 1000 ), nonprime AS ( SELECT N1.num FROM Numbers N1 JOIN Numbers N2 ON N2.num < N1.num AND N1.num % N2.num = 0 ) SELECT STRING_AGG(CAST(num AS VARCHAR), '&') AS concatenated_value FROM Numbers WHERE num NOT IN (SELECT num FROM nonprime) OPTION (MAXRECURSION 0);