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.
Used following in Oracle:
set serveroutput on;
DECLARE
num integer := 1;
i number;
c number;
op varchar(1000) := NULL;
BEGIN
for num in 2..1000 loop
c := 0;
for i in 2..(num-1) loop
if (num mod i =0) then c:=c+1;
exit;
end if;
end loop;
if (c=0)
then
if (op is null)
then op:= op||num;
else op:= op||'&'||num;
end if;
end if;
end loop;
dbms_output.put_line(op);
END;
/
Using exit for better performance. To make it even better, one can skip all even numbers.
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 →
Used following in Oracle: set serveroutput on; DECLARE num integer := 1; i number; c number; op varchar(1000) := NULL; BEGIN for num in 2..1000 loop c := 0; for i in 2..(num-1) loop if (num mod i =0) then c:=c+1; exit; end if; end loop; if (c=0) then if (op is null) then op:= op||num; else op:= op||'&'||num; end if; end if; end loop; dbms_output.put_line(op); END; /
Using exit for better performance. To make it even better, one can skip all even numbers.