如何让我的JavaScript程序计算出更多的e数字?

时间:2014-01-16 21:23:59

标签: javascript math

我一直在研究计算 e 的程序。这是on jsfiddle

/*
|| formula =
||
||          ∞
||         ____
||         \    (3k)^2 +1
||  e =    /    ---------
||         ____    (3k)!
||         k = 0
||
*/ 
var f = [];
function factorial (n) {
  if (n == 0 || n == 1)
    return 1;
  if (f[n] > 0)
    return f[n];
  return f[n] = factorial(n-1) * n;
}
var eCalc = {
    calculate: function(){
        var steps = document.eCalc.num.value;
        return this.calc(steps);
    },
    calc: function(terms){
        var start_time = Date.now(),
            e = 0,
            d,
            n;
        for(i = 0; i < terms; i++){
            n = Math.pow(3 * i, 2) + 1;
            d = factorial(3 * i);
            e = e + (n / d);
        };
        var end_time = Date.now(),
            total_time = end_time - start_time;
        if(total_time >= 1000){
            total_time = (total_time/1000)+"seconds";
        }else{
            total_time += "ms";
        }
        document.eCalc.result.value = e;
        document.getElementById('time').innerHTML = "Result calculated in: " + total_time + "...";
    }
}

如何让我的程序输出 e 的更多数字?

0 个答案:

没有答案