Here's some pseudocode:
for i = 1 to n do
for j = 1 to 2n do
x:= x + 5
for k = 1 to 5 do
x:= x+ k+ 1
So I found that 2n^2 + 10n addition operations performed by the algorithm. Now, if x has an initial value of 3, what will its final value be? My friend and I disagree on this one, so can anyone lay it out for us? Thanks in advance.
for i = 1 to n do
for j = 1 to 2n do
x:= x + 5
for k = 1 to 5 do
x:= x+ k+ 1
So I found that 2n^2 + 10n addition operations performed by the algorithm. Now, if x has an initial value of 3, what will its final value be? My friend and I disagree on this one, so can anyone lay it out for us? Thanks in advance.
-
Start with x: after adding 5 (2n^2) times
we have x + 10n^2
In the for k loop, we add 2 + 3 + 4 + 5 + 6 (for the 5 'k+1's) = 20
So we have as the final result x + 20 + 10n^2
x = 3
n: . 3 + . 10 n^2 ... + 20 = final result
1: .. 3 + . 10 + 20 = 33
2: ...3 + . 40 + 20 = 63
3: .. 3 + . 90 + 20 = 113
4: .. 3 + . 160 + 20 = 183
and so on.
we have x + 10n^2
In the for k loop, we add 2 + 3 + 4 + 5 + 6 (for the 5 'k+1's) = 20
So we have as the final result x + 20 + 10n^2
x = 3
n: . 3 + . 10 n^2 ... + 20 = final result
1: .. 3 + . 10 + 20 = 33
2: ...3 + . 40 + 20 = 63
3: .. 3 + . 90 + 20 = 113
4: .. 3 + . 160 + 20 = 183
and so on.