Description (题目)
请判断某数是否为质数。
Input (输入)
一个整数x, x>= 2 且 x<=2147483647
Output (输出)
如果x是质数,则输出“x is a prime number.”,否则输出“x is not a prime number.”,
Sample Input (例子,输入)
13
Sample Output (例子,输出)
13 is a prime number.
问问,我这里哪里错了?正确答案应该是怎么样的?
program sushu;
var
m,k,t:integer;
x:longint;
begin
readln(x);
t:=0;
for k:=1 to x do
if (x mod k=0) then t:=t+1;
if (t>2)or(t=1)or(t=0) then
write(x,' ','is not a prime number.');
if (t=2)then
write(x,' ','is a prime number.');
end.
var n:longint;function f(a:longint):boolean;var i:longint;begin f:=true; for i:=2 to trunc(sqrt(a)) do if a mod i=0 then begin f:=false; break; end; if (a=0) or (a=1) then f:=false;end;begin readln(n); if f(n)=true then writeln(n,' is a prime number.') else writeln(n,' is not a prime number.');end.已过,放心采纳
program work
var
total,i,j:integer;
begin
total:=2;
for i:=2 to 100 do//遍历每个数
for j:=3 to trunc(sqrt(i)) do
if i mod j then //判断是否为素数
total:=total+i;
writeln(total);
end.
一个合数有两个以上的约数 所以应该改成
if t > 2 then writeln( x , ' is not a prime number,' )
else writeln( x , ' is a prime number,' );
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息