SQL PLSQL 打印素数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27258291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PLSQL Printing prime numbers
提问by KPavezC
I want to print prime numbers between 1 to 50
. But I don't understand what I am doing wrong in my code. After BEGIN
, SQLDeveloper says I had an error because it expected another sign and not =
.
我想在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;
回答by Multisync
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 is NUMBER, by default it's NUMBER(38, max_scale))
k := n/2;
-- 添加了 FLOOR(k 是 NUMBER,默认情况下它是 NUMBER(38, max_scale))
IF (n%i := 0 ) THEN
-> IF (mod(n, i) = 0 ) THEN
IF (n%i := 0 ) THEN
-> IF (mod(n, i) = 0 ) THEN
Oracle has MOD function for remainder + to compare you need to use =
,
Oracle有余数+的MOD函数来比较你需要使用=
,
:=
is for assignment.
:=
是为了赋值。
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;
回答by 565r675t
You should create or replace in your source code:
您应该在源代码中创建或替换:
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;
/
回答by Sahana parvin suma
--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;
/
回答by Sahana parvin suma
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;
回答by Shankar
In your IF clause you are assigning the value instead of comparison. You are using := operator, where you shd be using =
在您的 IF 子句中,您正在分配值而不是比较。您正在使用 := 运算符,而您正在使用 =
It shud be like (IF counter = 0) then ......
它应该像(IF counter = 0)然后......
Also I dont think n%i would work, you could do IF (trunc(n) = n) then ... or IF (mod (n,i) =0) then ...
另外我不认为 n%i 会起作用,你可以做 IF (trunc(n) = n) then ... 或 IF (mod (n,i) =0) then ...
回答by Simon UK
Why don't you just check for previous prime divisibility?
你为什么不检查以前的素数整除性?
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
You could certainly cap the numbers you are checking against in the where condition to reduce your overheads further.
您当然可以在 where 条件中限制您正在检查的数字,以进一步减少您的开销。
回答by Sumit Kumar Gupta
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;
/
回答by Sahana parvin suma
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;
/