oracle 打印素数

3yhwsihp  于 2023-05-16  发布在  Oracle
关注(0)|答案(9)|浏览(131)

我想打印1 to 50之间的素数。但我不明白我在代码中做错了什么。在BEGIN之后,SQLEdeveloper说我有一个错误,因为它需要另一个符号,而不是=

SET SERVEROUTPUT ON
DECLARE
    i NUMBER;
    counter NUMBER;
    n NUMBER;
    k NUMBER;
BEGIN
    i := 2;
    counter := 0;
    n := 50;
    k := n/2;
    FOR i IN 1..k LOOP
        IF (n%i := 0 ) THEN
            counter := 1;
        END IF;
        IF (counter := 0) THEN
            DBMS_OUTPUT.PUT_LINE(n||' is prime number');
        END IF;
    END LOOP;
END;
af7jpaap

af7jpaap1#

SET SERVEROUTPUT ON
DECLARE
    i NUMBER;
    counter NUMBER;
    n NUMBER;
    k NUMBER;
BEGIN
    i := 2;
    counter := 0;
    n := 50;
    k := floor(n/2);
    FOR i IN 1..k LOOP
        IF (mod(n, i) = 0 ) THEN
            counter := 1;
        END IF;
        IF (counter = 0) THEN
            DBMS_OUTPUT.PUT_LINE(n||' is prime number');
        END IF;
    END LOOP;
END;

k := n/2;-- added FLOOR(k为NUMBER,默认为NUMBER(38,max_scale))
IF (n%i := 0 ) THEN -> IF (mod(n, i) = 0 ) THEN
Oracle有余数+比较的MOD函数,需要使用=
:=用于赋值。

DECLARE
    counter NUMBER;
    k NUMBER;
BEGIN
  FOR n IN 1..50 LOOP   
    counter := 0;
    k := floor(n/2);
    FOR i IN 2..k LOOP
        IF (mod(n, i) = 0 ) THEN
            counter := 1;
        END IF;
    END LOOP;
    IF (counter = 0) THEN
       DBMS_OUTPUT.PUT_LINE(n||' is prime number');
    END IF;
  END LOOP;
END;
xpcnnkqh

xpcnnkqh2#

在IF子句中,是赋值而不是比较。你正在使用:=运算符,这里你应该使用=
它应该像(如果计数器= 0)那么……
我也不认为n%i会工作,你可以做IF(trunc(n)= n)然后...或如果(mod(n,i)=0),则…

uklbhaso

uklbhaso3#

--this function is check prime number.
       create or replace function prime_a(x number) return
       varchar2 is
       n integer;
       ans varchar2(50);
       begin
       n:=(x/2);
       for i in 2..n loop 
       if mod(x,i)=0
       then ans:='not a prime';
       exit;
       else ans:='prime';
       end if;
       end loop;
       return ans;
       end;
       /
b1uwtaje

b1uwtaje4#

step-1:
  create table tob(prime number);
 step-2:
 create or replace procedure ro(m number,n number) 
 is
 a integer;
 co Boolean;
 begin
 for j in m..n loop
 co:=false;
 co:=(j=1 );
 a:=(j/2);
 for i in 2..a loop 
 co:=(mod(j,i)=0);
 exit when co;
 end loop;
 if(not co) then
 insert into tob values(J);
 end if;
 end loop;
 commit;
 end;
 /
 step-3:
   exec ro(1,50);

 step-4: check:-
 select * from tob;
9bfwbjaz

9bfwbjaz5#

您应该在源代码中创建或替换:

function prime_a(x number) return
   varchar2 is
   n integer;
   ans varchar2(50);
   begin
   n:=(x/2);
   for i in 2..n loop 
   if mod(x,i)=0
   then ans:='not a prime';
   exit;
   else ans:='prime';
   end if;
   end loop;
   return ans;
   end;
   /
dy2hfwbg

dy2hfwbg6#

你为什么不检查一下之前的素数整除性呢?

create table prime (primeno bigint)
declare @counter bigint
set @counter = 2
while @counter < 1000000
begin
if not exists(select top 1 primeno from prime where @counter % primeno = 0)
   insert into prime select @counter
set @counter = @counter + 1
end

select * from prime order by 1

您当然可以限制在where条件下检查的数字,以进一步减少开销。

js4nwp54

js4nwp547#

declare
  i number;
  j number;
  k number:=0;
begin 
  for i in 1..50 
  loop
    for j in 2..i-1
    loop
      if mod(i,j)=0 then
        k:=1;
        exit;
      end if;
    end loop;
    if k=0 then
      dbms_output.put_line(i);
    end if;
    k:=0;
  end loop;
end;
/
qmelpv7a

qmelpv7a8#

declare
  y number := 0;
begin
  for i in 1 .. 50 loop
    for x in 1 .. i loop
      if mod(i, x) = 0 then
        y := y + 1;
      end if;
    end loop;
    if y <= 2 then
      dbms_output.put_line(i);
    end if;
    y := 0;
  end loop;
end;
wvmv3b1j

wvmv3b1j9#

create or replace function prime_a(x number) return varchar2 is
     n integer;
     ans varchar2(50);
   begin
     n:=(x/2);
     for i in 2..n loop 
       if mod(x,i)=0 then 
          ans:='not a prime';
          exit;
       else 
          ans:='prime';
       end if;
     end loop;
     return ans;
   end;
   /

相关问题