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.
Print Prime Numbers
Print Prime Numbers
Sort by
recency
|
1287 Discussions
|
Please Login in order to post a comment
CREATE TABLE t ( Colm int, ); declare @x int =2 declare @i int declare @b int declare @z int while @x >=2 and @x<=1000 begin set @i=@x set @b=2 set @z=0 while @b < @i begin if @i%@b=0 set @z=@z+1 set @b=@b+1 end if @z=0 INSERT INTO t (colm) VALUES (@i);
end select STRING_AGG(colm, '&') clom from t
WITH Numbers AS ( SELECT 2 AS num UNION ALL SELECT num + 1 FROM Numbers WHERE num < 1000 ), nonprime AS ( SELECT N1.num FROM Numbers N1 JOIN Numbers N2 ON N2.num < N1.num AND N1.num % N2.num = 0 ) SELECT STRING_AGG(CAST(num AS VARCHAR), '&') AS concatenated_value FROM Numbers WHERE num NOT IN (SELECT num FROM nonprime) OPTION (MAXRECURSION 0);
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; /
WITH A AS ( SELECT LEVEL AS L FROM DUAL CONNECT BY LEVEL <= 1000 ) SELECT LISTAGG(L,'&') WITHIN GROUP (ORDER BY L) FROM ( SELECT L FROM A MAIN WHERE NOT EXISTS ( SELECT 1 FROM A SUB WHERE MAIN.L>SUB.L AND MOD(MAIN.L,L)=0 AND SUB.L>1 ) AND L<>1 );