day13
<script> 2 var temp = 0; 3 for (var i = 2; i <= 1000; i++) { //4 4 //假设所有的数都是质数 5 var flag = true; 6 //通过嵌套循环找到 i 除了1 和本身以外所有可能出现的因子 7 for (var j = 2; j <= i / 2; j++) { 8 //判断 i 是否为质数 9 if (i % j == 0) { //能进到当前的分支 说明不是质数 10 flag = false; 11 break; 12 } 13 } 14 15 if (flag == true) { //if (flag) { 16 // console.log(i); 17 temp += i; 18 } 19 } 20 console.log(temp) 21 </script> 22 23 <script> 24 var f = false; 25 var i = 2; 26 var j = 2; 27 var n = 0; 28 while (i < 1000) { 29 f = true; 30 while (j <= i / 2) { 31 if (i % j == 0) { 32 f = false; 33 break; 34 } 35 j++; 36 } 37 38 if (f) { 39 40 n += i 41 } 42 j = 2; 43 i++; 44 } 45 document.write(n); 46 </script>
结果:76127
2-打印出 1-10000 之间所有对称数(121 1221)
1 <script> 2 //方法一 3 function isSymmetryNum(start, end) { 4 var flag; 5 for (var i = start; i < end + 1; i++) { 6 flag = true; 7 var iStr = i.toString(); 8 for (var j = 0, len = iStr.length; j < len / 2; j++) { 9 if (iStr.charAt(j) !== iStr.charAt(len - 1 - j) || i < 10) { 10 flag = false; 11 break; 12 } 13 } 14 if (flag == true) { 15 console.log(i); 16 } 17 18 } 19 } 20 //方法二 21 /* function isSymmetryNum(start, end) { 22 for (var i = start; i < end + 1; i++) { 23 var iInversionNumber = +(i.toString().split("").reverse().join("")); 24 25 if (iInversionNumber === i && i > 10) { 26 console.log(i); 27 } 28 29 } 30 } */ 31 isSymmetryNum(1, 10000); 32 </script>
1 <!DOCTYPE html> 2 3 <html> 4 5 <head> 6 7 <meta charset="utf-8" /> 8 9 <title></title> 10 11 </head> 12 13 <body> 14 15 <p>斐波那契数列:1,1,2,3,5,8,13,21,34,55,89,144........... </p> 16 17 <p>求斐波那契数列第n项的值</p> 18 19 </body> 20 21 <script type="text/javascript"> 22 23 var n = prompt("请输入数字n") 24 25 var num1 = 1; 26 27 var num2 = 1; 28 29 for(var i = 3; i <= n; i++) { 30 31 var temp = num2; 32 33 num2 = num1 + num2; 34 35 num1 = temp; 36 37 } 38 39 alert(num2); 40 41 </script> 42 43 </html>