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

Người đăng: share-nhungdieuhay on Thứ Sáu, 8 tháng 11, 2013

<!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>

{ 0 nhận xét... read them below or add one }

Đăng nhận xét