[r]
Trang 1BÀI TẬP VỀ SỐ NGUYÊN TỐ
Bài 1:Cho trước số tự nhiên n Lập
trình phân tích n thành tích các thừa
số nguyên tố.
Program P3203;
Uses CRT;
Var N, i: word;
Begin
write(' Cho số tự nhiên N: '); readln(N);
writeln(N, '=');
i:=2
While N>1 do
Begin
if N div i = 0 then
Begin
write(i, '*');
N:= N div i;
end
else
inc(i);
end;
readln;
End.
Bài 2 Hãy lập trình nhận biết một số
có phải là số nguyên tố hay không?
Program P3201;
Uses CRT;
Var n, i: integer;
Begin write('Hãy nhập một số tự nhiên: ); readln(n);
i:=2;
While (i<= SQRT(n)) and (n mod i>0)
do inc(i);
if i> SQRT(n) then writeln(n, 'là số nguyên tố ') else
writeln(n, ' là hợp số');
End.
Bài 3 :Tìm số lớn nhất trong 4 sô
Program So_lon;
Var so1,so2,so3,so4,solon1,solon2,sol on:Integer;
Begin
Writeln('TIM SO LON NHAT TRONG 4 SO NGUYEN');
Writeln('Cau truc If Then Else');
Writeln(' -');
Write('-Nhap so thu nhat: '); Readln(so1);
Write('-Nhap so thu hai : ');
Trang 2Readln(so2);
Write('-Nhap so thu ba : '); Readln(so3);
Write('-Nhap so thu tu : '); Readln(so4);
If so1 > so2 Then
solon1:= so1
Else
Solon1:=so2;
If so3 > so4 Then
solon2:=so3
Else
solon2:=so4;
If solon1 > solon2 Then
solon:=solon1 Else
solon:=solon2;
Writeln;
Writeln('+So lon nhat trong
4 so la: ',solon);
Writeln;
Writeln('Bam phim <Enter>
de ket thuc');
Readln
End
Bai 4 Phân tích số ra thừa số nguyên tố
Program thuaso;
Var
n,i:Integer;
Begin
Writeln('PHAN TICH N THANH TICH CAC THUA SO NGUYEN TO');
Writeln(' -');
Write('-Nhap so N= ');
Readln(n);
Repeat
i:=2;
While (n Mod i <> 0) And (i
< n ) Do
Write(i:4);
n:=n Div i;
Until n=1;
Writeln;
Writeln(' Bam phim <Enter>
de ket thuc');
Readln
End