I have the book saying that

and says that this is equivalent to saying
for(i = 1; i <= N; i++)
Sum += i;
and it further says utilising this simple formula,

since (maximum value - minimum value + 1 )
and it says changing this to C code would be
for(k = j; k <= 1; k++)
Sum += k;
However, I seriously cannot understand this! can anyone explain this to me?????
Thank you very much
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With