序列(阶乘)javascript的乘积 [英] product of a sequence (factorial) javascript

查看:51
本文介绍了序列(阶乘)javascript的乘积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个想法是找到一个序列中的数字.公式图片:

The idea is to find numbers in a sequence.. equation pic:

var pat = document.getElementById("pattern");
var nMax = 96; 

for (var n = 2; n <= 96; n++) {
    if (n != 96)
    	pat.innerHTML += 1000 * (999 - 10 * (n - 2))/(1000 - 10 * (n - 2)) + ", ";
    else
        pat.innerHTML += 1000 * (999 - 10 * (n - 2))/(1000 - 10 * (n - 2)) + ",&hellip;";
}

<body>
    <p id="pattern"></p> 
</body>

但是问题是pat.innerHTML不会将所有前n个数字相乘.这个想法是创建一个序列:

But the problem is pat.innerHTML doesn't multiply all previous n numbers. The idea is to create a sequence:

a(2) = 1000 * (999 - 10 * (2 - 2))/(1000 - 10 * (2 - 2)) 
a(3) = 1000 * (999 - 10 * (2 - 2))/(1000 - 10 * (2 - 2)) * (999 - 10 * (3 - 2))/(1000 - 10 * (3 - 2))
a(4) = a(3) * (999 - 10 * (4 - 2))/(1000 - 10 * (4 - 2))

等.我怎么做?(有关正确的数学公式,请参见图片.)

etc.. How do I do that? (See pic for the equation in proper math notation.)

推荐答案

听起来,您只需跟踪保存上一次迭代结果的持久变量,然后乘以以计算当前迭代的结果它.该序列从1000开始,因此将其作为初始结果:

It sounds like you just need to keep track of a persistent variable that holds the result from the last iteration, and calculate the result for the current iteration by multiplying by it. The sequence starts at 1000, so put that as the initial result:

var pat = document.getElementById("pattern");
let lastResult = 1000;
for (var n = 2; n <= 94; n++) {
  const nextResult = lastResult * ((999 - 10 * (n - 2))/(1000 - 10 * (n - 2)))
  pat.innerHTML += nextResult + '<br>';
  lastResult = nextResult;
}

<body>
    <p id="pattern"></p> 
</body>

这篇关于序列(阶乘)javascript的乘积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆