I have the book saying that
and says that this is equivalent to saying
About the second sum, it starts at k=j
and ends at i
. That sum is not equal to (j - i - 1)
, instead, it's equal to (i - j + 1)
. Let's do an example :
If j = 3
and i = 6
, then k = 3
and sum = 1+1+1+1 = 4
. By applying the formula : sum = (i - j + 1) = 6 - 3 + 1 = 4
.
Now, the C code for the first example, they said :
for(i = 1; i <= N; i++) Sum += i;
This is wrong. Instead, it should be :
for(i = 1; i <= N; i++)
Sum += 1;
In the second one, they said :
for(k = j; k <= 1; k++) Sum += k;
Instead, it should be :
for(k = j; k <= i; k++)
Sum += 1; // Where i >= j