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.
SET SERVEROUTPUT ON;
DECLARE
prime_ls SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST();
is_prime BOOLEAN;
rst_str VARCHAR2(4000) := '';
BEGIN
FOR i IN 2..1000 LOOP
is_prime := TRUE;
FOR J IN 2..FLOOR(SQRT(i)) LOOP
IF MOD(i, j) = 0 THEN
is_prime := false;
exit;
END IF;
END LOOP;
IF is_prime THEN
prime_ls.EXTEND;
prime_ls(prime_ls.LAST) := TO_CHAR(i);
END IF;
END LOOP;
FOR idx in 1..prime_ls.COUNT LOOP
IF idx > 1 THEN
rst_str := rst_str || '&';
END IF;
rst_str := rst_str || prime_ls(idx);
END LOOP;
DBMS_OUTPUT.PUT_LINE(rst_str);
END;
/
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 →
SET SERVEROUTPUT ON; DECLARE prime_ls SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST(); is_prime BOOLEAN; rst_str VARCHAR2(4000) := ''; BEGIN FOR i IN 2..1000 LOOP is_prime := TRUE; FOR J IN 2..FLOOR(SQRT(i)) LOOP IF MOD(i, j) = 0 THEN is_prime := false; exit; END IF; END LOOP; IF is_prime THEN prime_ls.EXTEND; prime_ls(prime_ls.LAST) := TO_CHAR(i); END IF; END LOOP; FOR idx in 1..prime_ls.COUNT LOOP IF idx > 1 THEN rst_str := rst_str || '&'; END IF; rst_str := rst_str || prime_ls(idx); END LOOP; DBMS_OUTPUT.PUT_LINE(rst_str); END; /