I'm completing some time complexity analysis on loops which requires me to represent psuedocode loops as summations. I have two summations that I believe I have done to a degree of correctness, but am unsure. Here's the initial pseudocode for the first:
for i=1 to n:
for j=1 to n:
if i+j<n:
for k=1 to n:
sum++
I take this to mean the following:
$\sum_{i=1}^n\sum_{j<n-i}^n\sum_{k=1}^n 1$
$\sum_{i=1}^n\sum_{j<n-i}^nn = \sum_{i=1}^n n\sum_{j<n-i}^n$
$\sum_{i=1}^n n(n-(n-i)+1) = n\sum_{i=1}^n 1-i = n(\sum_{i=1}^n 1 - \sum_{i=1}^n i)$
$n(n-\frac{n(n+1)}{2}) = n^2-\frac{n^3}{2}+\frac{n^2}{2}$
So the overall time complexity of this loop (in the worst case) is $O(n^3)$. Is this accurate? I'm concern that the upper bound for my second summation is incorrect.
The second psuedocode is this:
for i=1 to n:
for k=1 to n:
sum++
k=k/2
This psuedocode contains no tricky 'if' statement, but the $k=\frac{k}{2}$ confuses me a bit. Here's the summation I have:
$\sum_{i=1}^n\sum_{k=1}^n 1+\frac{k}{2} = \sum_{i=1}^n 1 + \frac{1}{2}\sum_{k=1}^n k$
$\sum_{i=1}^n n+\frac{n(n+1)}{4} = \sum_{i=1}^n n+ \sum_{i=1}^n\frac{n(n+1)}{4} = n\sum_{i=1}^n 1 + \frac{n^2}{4}\sum_{i=1}^n 1 + \frac{n}{4}\sum_{i=1}^n 1$
$n^2+\frac{n^3}{4} + \frac{n^2}{4}$
So the time complexity is $O(n^3)$. Again, I'm not sure if I handled that $k=\frac{k}{2}$ properly, so feedback would be appreciated!