判断质数,1到一百内所有质数 js

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        for (var i=3;i<=100;i++){
            var flag=true;
            for(var j=2;j<i;j++){
                if(i%j===0){
            flag=false
        }
            }
        if(flag){
            console.log(i+'是质数')
        }
        else{
            console.log(i+'不是质数')
        }
        }
    </script>
</body>
</html>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script>
    var num=20;
    var flag=true;
    for (var i=2;i<num;i++ ){
        if(num %i ==0){
            flag=false
        }
    }
    flag ? console.log(num+'是质数'):console.log(num+'不是质数')
</script>
</body>
</html>
posted @ 2020-10-27 19:41  无敌臭弟弟  阅读(233)  评论(0编辑  收藏  举报