Created
December 5, 2016 05:30
-
-
Save tuankiet65/b161051fda155005eb50cebcb2f83b44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program songuyeno; | |
uses crt; | |
var n,nt,k:longint; | |
begin | |
clrscr; // xoá màn hình | |
write('n = '); | |
readln(n); // nhập vào số cần kiểm tra | |
if (n = 2) or (n = 3) then // Ví dụ như n là 2 hoặc 3 thì là số nguyên tố | |
nt:=0 // nt = 0 thì n là số nguyên tố, 1 nếu ngược lại | |
else | |
if (n mod 2 = 0) or (n mod 3 = 0) then // nếu n chia hết cho 2 cho 3 thì không phải số nguyên tố | |
nt:=1 // gán nt = 1 vì n không là số nguyên tố | |
else begin // vào vòng kiểm tra | |
nt:=0; | |
k:=5; | |
while k <= trunc(sqrt(n)) do begin // trunc(sqrt(n)) là lấy phần nguyên của căn 2 của n | |
if (n mod k = 0) or (n mod (k+2) = 0) then | |
nt:=1; | |
break; | |
k:=k+6; | |
end; | |
end; | |
if nt=0 then | |
write(n,' la so nguyen to') | |
else | |
write(n,' khong phai la so nguyen to'); | |
readln | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment