I'm trying to understand Big O and thought stepping through a simple program might help.
def sum(n):
k = 0
j = 0
while k < n:
k = k + 1
while j < n:
j = j + 1
k = k + j
return k
k and j are initially assigned the value 0, which counts for 2 assingments, the first while loop performs 1 assignment n times and second while performs 2 assignments n times. So the expression would be 2 + n + 2n.
Because the first two terms in the above expression (2 and n) are constant, they will become insignificant compared to the third term, which multiples n by 2 as n grows. So the Big O for the code would be O(2n).
Is my logic and answer correct? Thanks in advance