1+2+3+...+100怎么算最快?
 run = function () {
   var i = 0
   return function () {
     while (i++ < 4) {
       eval(util.format('r=oneAddTohundredAlgorithm%d()', i))
       log('algorithm%d=%d', i, r)
     }
   }
 }()
 run()

 function oneAddTohundredAlgorithm1() {
   var sum = 0
   for (let i = 1; i <= 100; i++) {
     sum += i
   }
   return sum
 }

 function oneAddTohundredAlgorithm2() {
   var max = 100
   var sum = 0
   for (let i = 0; i < max / 2; ++i) {
     sum += (i + 1) + (max - i)
   }
   return sum
 }

 function oneAddTohundredAlgorithm3() {
   var start = 1
   var end = 100
   var sum = (start + end) * 100 / 2
   return sum
 }

 function oneAddTohundredAlgorithm4() {
   return 5050
 }

第四个最快,直接返回
还有等差数列可以计算啊

  • 4
    帖子
  • 1766
    浏览

与 Auto.js 的连接断开,我们正在尝试重连,请耐心等待