PLSQL打印素数

时间:2014-12-02 20:26:05

标签: sql oracle plsql

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

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;

8 个答案:

答案 0 :(得分:2)

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; - 添加了FLOOR(k为NUMBER,默认为#NUM;(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;

答案 1 :(得分:0)

在您的IF子句中,您要分配值而不是比较。您正在使用:=运算符,您可以使用=

然后就像(IF计数器= 0)那样 ......

另外我不认为n%我会工作,你可以做IF(trunc(n)= n)然后......或IF(mod(n,i)= 0)然后......

答案 2 :(得分:0)

       --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;
       /

答案 3 :(得分:0)

 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;        

答案 4 :(得分:0)

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

 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;
   /

答案 5 :(得分:-1)

为什么不检查以前的主要可分性?

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条件中限制您要检查的数字,以进一步减少您的管理费用。

答案 6 :(得分:-1)

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;
/

答案 7 :(得分:-3)

   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;
   /