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
),
Divisors AS (
SELECT 2 AS d
UNION ALL
SELECT d + 1 FROM Divisors WHERE d < 31 -- 31 is approx sqrt(1000)
)
SELECT GROUP_CONCAT(n SEPARATOR '&') AS PrimeNumbers
FROM Numbers
WHERE NOT EXISTS (
SELECT 1 FROM Divisors
WHERE Divisors.d < Numbers.n
AND Numbers.n % Divisors.d = 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 RECURSIVE Numbers AS ( SELECT 2 AS n UNION ALL SELECT n + 1 FROM Numbers WHERE n < 1000 ), Divisors AS ( SELECT 2 AS d UNION ALL SELECT d + 1 FROM Divisors WHERE d < 31 -- 31 is approx sqrt(1000) ) SELECT GROUP_CONCAT(n SEPARATOR '&') AS PrimeNumbers FROM Numbers WHERE NOT EXISTS ( SELECT 1 FROM Divisors WHERE Divisors.d < Numbers.n AND Numbers.n % Divisors.d = 0 );