Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't understand the sigma notation and for loop

I have the book saying that

enter image description here

and says that this is equivalent to saying

for(i = 1; i <= N; i++)
    Sum += i;

and it further says utilising this simple formula,

enter image description here

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

like image 575
Jane Doe Avatar asked Nov 20 '13 13:11

Jane Doe


1 Answers

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
like image 169
Gabriel L. Avatar answered Oct 21 '22 22:10

Gabriel L.