在for循环中查找变量迭代的总和 [英] Find sum of iterations of variable in for loop

查看:122
本文介绍了在for循环中查找变量迭代的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(i = 0; i< = 1000; i ++){
if(i%3 === 0){
console.log(code $)一世);
}
if(i%5 === 0){
console.log(i);
}

}

我想添加每个输出 i 在一起。 即。 0 + 0 + 3 + 5 + 6 + 9 + 10 ... + 1000

有没有算法可以做到这一点,我只是开始把这些数字中的每一个加起来?

解决方案

从1到 n

  n *(n + 1)/ 2 

可以被3整除的从1到1000的数字之和等于从1到 1000/3 乘以3.类似地,可以被5整除的从1到1000的数字之和与从1到 1000/5 ,乘以5。

我打赌你正在处理的问题是要排除可被15整除的数字: p>

编辑—为什么这个工作?那么,考虑从1到 n 的简单情况;比如1到100

$ $ $ $ $ $ $ $ 1,2,3,4,5,... 97,98,99,100

现在,考虑一下相同的数字列表,但是倒退:

  100,99,98,97,... 4,3,2,1 

请注意,当我们从这两个列表中添加对时,我们总是得到101:

  100 + 1是101 
99 + 2是101
98 + 3是101
...
4 + 97是101
3 + 98是101
2 + 99是101
1 + 100是101

所有这一切都是一样的,即101.如果我们做乘法和除以2,我们已经得到了答案:)

现在,怎么样总和可以被3或5整除的数字?那么如果你想一想,这些数字是什么样的?

  3,6,9,12,... 993 ,996,999 

嗯...看起来很像

  3 *(1,2,3,4,... 331,332,333)
333 * 334/2 ,如果我们乘以3我们得到1到1000之间可以被3整除的数字的总和。同样是5。如果我们想放弃3和5可以整除的数字之和,我们将计算从1到 1000/15 从结果中减去



哦,还有一件事。如果我们谈论的是整数的总和,那么我们怎么知道我们除以2的那个步骤不会让我们有一小部分呢?那么记住,公式是 n *(n + 1)/ 2 。如果 n 是一个奇数,那么 n + 1 就是偶数。因此,这个乘法将始终包含一个偶数,所以除以2将永远不会让我们有一小部分!

for (i = 0; i <= 1000; i++) {
    if ( i % 3 === 0){
        console.log(i);
    }
    if ( i % 5 === 0){
        console.log(i);
    }

}

I want to add each output of i together. i.e. 0+0+3+5+6+9+10...+1000

Is there an algorithm to do this, or do I just start adding every single one of these numbers together?

解决方案

The sum of the numbers from 1 up to n is

n * (n + 1) / 2

The sum of the numbers from 1 up to 1000 that are divisible by 3 is the same as the sum of the numbers from one up to 1000 / 3, multiplied by 3. Similarly, the sum of the numbers from 1 to 1000 that are divisible by 5 is the same as the numbers from 1 to 1000 / 5, multiplied by 5.

I bet the problem you're working on wants you to exclude numbers that are divisible by 15 :)

edit — Why does this work? Well, consider the simpler case of the numbers from 1 to n; say, 1 to 100.

1, 2, 3, 4, 5, ... 97, 98, 99, 100

Now, consider that same list of numbers, but backwards:

100, 99, 98, 97, ... 4, 3, 2, 1

Note than when we add pairs from those two lists, we always get 101:

100 + 1 is 101
99 + 2 is 101
98 + 3 is 101
...
4 + 97 is 101
3 + 98 is 101
2 + 99 is 101
1 + 100 is 101

So there are 100 sums all the same, that being 101. If we do that multiplication and divide by 2, we've got the answer :)

Now, what about the sum of the numbers divisible by 3, or 5? Well if you think about it, what do those numbers look like?

3, 6, 9, 12, ... 993, 996, 999

Hmm... that looks a lot like

3 * (1, 2, 3, 4, ... 331, 332, 333)

So the sum of the numbers 1 through 333 is 333 * 334 / 2, and if we multiply that by 3 we get the sum of the numbers from 1 to 1000 that are divisible by 3. Same goes for 5. If we want to drop the sum of the numbers divisible by both 3 and 5, we'd compute the sum of the numbers from 1 to 1000 / 15 and subtract that from the result.

Oh, and one more thing. If we're talking about a sum of integers, how do we know that that step where we divide by 2 won't leave us with a fraction? Well, the formula is n * (n + 1) / 2, remember. If n is an odd number, then n + 1 is even. Thus, that multiplication will always involve one even number, so dividing by 2 will never leave us with a fraction!

这篇关于在for循环中查找变量迭代的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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