Recent Posts

JavaScript: Kiểm tra số n (nguyên, nhập từ bàn phím) có phải số nguyên tố không ?

-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script>
var i,n,nt=1;
function nguyento(x){
if(x==1 || x==2) nt=1;
if(x>2){
for(i=2;i<=Math.sqrt(x);i++){
if(x%i==0){nt=0; break;}
}
}
return nt;
}
n=prompt("Nhap gia tri n:");
if(nguyento(n)==1) document.write(n + " LA so nguyen to")
else document.write(n + " KHONG la so nguyen to")
</script>
</body>
</html>




Klik untuk melihat kode: :) =( :s :D :-D ^:D ^o^ 7:( :Q :p T_T @@, :-a :W *fck* x@