Home > Web Front-end > Front-end Q&A > How to find prime numbers from 1 to 100 in javascript

How to find prime numbers from 1 to 100 in javascript

WBOY
Release: 2022-01-17 16:51:37
Original
7434 people have browsed it

Method: 1. Use "for(var i=2;i

How to find prime numbers from 1 to 100 in javascript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How to find prime numbers from 1 to 100 in javascript

To find prime numbers, you must first understand the concept of prime numbers: except itself and 1, other numbers They are all inexhaustible.

The example is as follows:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>求1到100之间所有的质数</title>
    </head>
    <body>
        <script type="text/javascript">
            for (var i=2;i<=100;i++) {
                //假设i是质数,
                var b=true;
                //求证的过程,在1到i之间有没有能被整数整除的数
                for (var j=2;j<i;j++) {
                    if(i%j==0){
                        //有数被整除了,就证明这个数不是质数
                        b=false;
                        break;
                    }
                }
                if(b==true){
                    document.write(i+"<br />")
                }
            }
        </script>
    </body>
</html>
Copy after login

Output result:

How to find prime numbers from 1 to 100 in javascript

##[Related recommendations:

javascript learning tutorial

The above is the detailed content of How to find prime numbers from 1 to 100 in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template