js中求水仙花數


 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <script type="text/javascript">
 7             //水仙花數
 8             for (var num = 100; num <= 999; num++) {
 9 
10                     var str = num.toString();
11                     var res1 =Number(str.slice(0,1)) //百位   或者用charAt(0)
12                     var res2 =Number(str.slice(1,2)) //十位        或者用charAt(1)
13                     var res3 =Number(str.slice(2,3)) //個位        或者用charAt(2)
14 
15                     var sumcube = Math.pow(res1,3)+Math.pow(res2,3)+Math.pow(res3,3); //立方和
16                     num = res1*100+res2*10+res3; //這個數
17                     if(sumcube == num){
18                         console.log(num);
19                     }
20             }
21     </script>
22 </head>
23 <body>
24     
25 </body>
26 </html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM