Method: 1. Use "for(var i=2;i
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>
Output result:
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!