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 RECURSIVE numbers AS (
SELECT 2 AS n
UNION ALL
SELECT n + 1 FROM numbers WHERE n < 1000
)
SELECT GROUP_CONCAT(n SEPARATOR '&') AS prime_numbers
FROM numbers
WHERE NOT EXISTS (
SELECT 1
FROM numbers AS d
WHERE d.n <= SQRT(numbers.n)
AND numbers.n % d.n = 0
AND d.n > 1
);
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 RECURSIVE numbers AS ( SELECT 2 AS n UNION ALL SELECT n + 1 FROM numbers WHERE n < 1000 ) SELECT GROUP_CONCAT(n SEPARATOR '&') AS prime_numbers FROM numbers WHERE NOT EXISTS ( SELECT 1 FROM numbers AS d WHERE d.n <= SQRT(numbers.n) AND numbers.n % d.n = 0 AND d.n > 1 );