pascal三道质数习题

来源:百度知道 编辑:UC知道 时间:2024/05/05 10:21:38
有一个质数,它加上10是质数,加上14也是质数,这个质数是多少?

一个质数加上6或减去6所得的数仍然是质数,在100以内这样的质数是哪几个?

两个合数的和是31,这两个合数分别是多少?

function check(x:integer):boolean;
var i:integer;
begin
if x<=1 then begin
check:=false;
exit;
end;
check:=true;
for i:=2 to int(sqrt(x)) do
if x mod i=0 then begin
check:=false;
break;
end;
end;

第一个:
i:=2;
while not(check(i) and check(i+10) and check(i+14)) do
inc(i);
writeln(i);

第二个
for i:=2 to 100 do
if check(i) and (check(i+6) or check(i-6)) then
writeln(i);

第三个
for i:=2 to 29 do begin
j:=31-i;
if (not check(i))and(not check(j)) then
writeln(i,' ',j);
end;

第一题:
var a,b,c,d:integer;
j,k,l:boolean;
begin
a:=0;
repeat
j:=true;
k:=true;
l:=true;
a:=a+1;
for b:=2 to a-1 do if a mod b=0 then j:=false;
for c:=2 to a+10-1 do if (a+10) mod c=0 then k:=false;
for d:=2 to a+14-1 do if (a+