Question About Summation of Summations

52 Views Asked by At

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!

1

There are 1 best solutions below

0
On

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*}

We obtain by first expanding the outer sum and then the inner sum: \begin{align*} \color{blue}{\sum_{i=2}^4}\color{blue}{\sum_{j=2}^i j} &=\sum_{j=2}^2j+\sum_{j=2}^3j+\sum_{j=2}^4j\\ &=(2)+(2+3)+(2+3+4)\\ &\,\,\color{blue}{=16} \end{align*}