# Number of steps in a nested loop

I am trying to calculate the number of steps executed for the following nested loop specially for asymptotic growth. Based on the number of steps I will derive the Big O for this algorithm.

``````def get_multiples(list):
multiple = []
for o in list:
for i in list:
multiple.append(o*i)
return multiple
```
```

The way I have calculated is as follows (list consists of large number of elements = "n"):

1. Assignment statement (no. of steps = 1):

````multiple = []`
```
2. Nested Loops:

``````for o in list:
for i in list:
multiple.append(o*i)
```
```

In the outer loop the variable o is assigned n times. Each time the outer loop executes, first the variable i is assigned n times, then the variables are multiplied n times and finally the list is appended n times. Therefore the no. of steps = n*(n+n+n) = 3n2

3. Return statement (No. of steps = 1):

````return multiple`
```

Therefore the total no. of steps = 3n2 + 2

However the correct answer is 3n2 + n +2. Apparently the execution of the outer loop takes additional n steps which is not required for the inner loop.

Can somebody explain to me what did I miss ?

It does not make a difference to complexity since it will still be O(n2)

I think that the correct way to calculate the nested loop is as follows:

The number `o` is assigned n times. the number `i` is assigned n2 times, o*i is calculated n2 times, the append function is called n2 times.

therefore n + n2 + n2 + n2 = 3n2 + n

add it to the rest and you get 3n2 + n + 2