I HAVE A QUICK QUESTION. I AM NOT SURE IF I AM LOOKING AT OR SOLVING THIS QUESTION RIGHT.

IF YOU HAVE
for(i=1, i<=n, i++)
for(int j=1, j<=i, j++)
for(int k=1, k<=5, k++)

1: for the k loop i take the summation of i=1 to 5 of Tsubi to get (5xT)

2: for the j loop i take the summation of i=1 to n of 5xTsubi to get n(5xT)

3: for the i loop i take the summation of i=1 to n of(n*5)xTsubi to get
(n*5)*n*T
or do i solve like this

1: for the k loop summation k=1 to 5 of Tsubk to get 5xT

2: for the j loop summation of j=1 to i of 5*Tsubj to get 5*i*T

3: for the i loop summation of i=1 to n of 5*i*Tsubi to get (n*5)T*(1+2+3+4....+n)

also you can see in the first one i took all the summation in terms of Tsubi and in the second example i took the summations in terms of the variable in the loop. which way is correct. any hints or tricks on how to look and analyze future algorithms also would be great thanks again.

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by AuburnMathTutor

How about running the code with print in loop and after it. Gives me best insight.

Second answer looks right, you work from inside the loop outwords.

Here is my test in Python my favourite language:

``````## coding the loop in python and adding prints
"""for(i=1, i<=n, i++)
for(int j=1, j<=i, j++)
for(int k=1, k<=5, k++)"""
n=3
for i in range(1,n+1):
print 'Next i (%i)' % i
for j in range(1,i+1):
print 'Next j (%i)' % j
for k in range(1,6):
print
print
"""Output:
Next i (1)
Next j (1)

Next i (2)
Next j (1)
Next j (2)

Next i (3)
Next j (1)