kiểm tra n có phải là số nguyên tố không ?kiểm tra n có hoàn toàn hay không ?xuất ra màn hình các số nguyên tố từ 2 đến n
0 bình luận về “kiểm tra n có phải là số nguyên tố không ?kiểm tra n có hoàn toàn hay không ?xuất ra màn hình các số nguyên tố từ 2 đến n”
Program BTT; Uses crt; Var n,i: longint;
Function ngto(a: longint): boolean; Var i: longint; Begin If a<2 then exit(false); For i:=2 to a div 2 do If a mod i = 0 then exit(false); exit(true); End;
Function shh(b: longint): boolean; Var i,s: longint; Begin s:=0; For i:=1 to b div 2 do If b mod i = 0 then s:=s+i; If s=b then exit(true); exit(false); End;
Begin Clrscr; Write(‘Nhap n: ‘); Readln(n); If ngto(n) then Write(n,’ la so nguyen to’) Else Write(n,’ khong phai la so nguyen to’); Writeln; If shh(n) then Write(n,’ la so hoan hao’) Else Write(n,’ khong phai la so hoan hao’); Writeln; Write(‘Cac so nguyen to tu 2 den n la: ‘); For i:=2 to n do If ngto(i) then Write(i,’ ‘); Readln; End.
Program BTT;
Uses crt;
Var n,i: longint;
Function ngto(a: longint): boolean;
Var i: longint;
Begin
If a<2 then exit(false);
For i:=2 to a div 2 do
If a mod i = 0 then exit(false);
exit(true);
End;
Function shh(b: longint): boolean;
Var i,s: longint;
Begin
s:=0;
For i:=1 to b div 2 do
If b mod i = 0 then s:=s+i;
If s=b then exit(true);
exit(false);
End;
Begin
Clrscr;
Write(‘Nhap n: ‘); Readln(n);
If ngto(n) then Write(n,’ la so nguyen to’)
Else Write(n,’ khong phai la so nguyen to’);
Writeln;
If shh(n) then Write(n,’ la so hoan hao’)
Else Write(n,’ khong phai la so hoan hao’);
Writeln;
Write(‘Cac so nguyen to tu 2 den n la: ‘);
For i:=2 to n do
If ngto(i) then Write(i,’ ‘);
Readln;
End.