a007.
判斷質數
| From: [180.169.25.90] |
發表日期
:
2015-04-18 17:03
var
hs:array[1..46340]of boolean;
zs:array[1..4792]of longint;
i,j,k:longint;
hh:boolean;
begin
for i:=2 to 46340 do begin
if not hs[i] then begin j:=j+1; zs[j]:=i; end;
k:=i+i;
while k<46341 do begin hs[k]:=true; k:=k+i; end;
end;
while not eof do begin
readln(j);
if j=2147483647 then writeln('質數')
else begin
k:=trunc(sqrt(j));
for i:=1 to 4792 do begin
if zs[i]>k then break;
if j mod zs[i]=0 then begin hh:=true; break; end;
end;
if hh then writeln('非質數')
else writeln('質數');
hh:=false;
end;
end;
end.