• + 2 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.