I am trying to figure out the value of $$\sum_{i=2}^4\sum_{j=2}^i j$$
I thought of having a two for loops, one nested inside of the other, so:
for(int i = 2; i <= 4; ++i) {
for(int j = 2; j <= i; ++j) {
sum += j;
}
}
So I got:
i = 2, j = 2: 2
i = 3, j = 2: 2 + 2
i = 3, j = 3: 2 + 2 + 3
i = 4, j = 2: 2 + 2 + 3 + 2
i = 4, j = 3: 2 + 2 + 3 + 2 + 3
i = 4, j = 4: 2 + 2 + 3 + 2 + 3 + 4 = 16
But I know this is the incorrect answer, and I'm not sure why.
Any help would be greatly appreciated!
Your calculations are fine. Recall the sigma operator $\sum$ works as follows: \begin{align*} \sum_{i=a}^bf(i)=f(a)+f(a+1)+\cdots+f(b) \end{align*}