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"):

Assignment statement (no. of steps =

**1**):`multiple = []`

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)**=**3n**^{2}Return statement (No. of steps =

**1**):`return multiple`

Therefore the total no. of steps = **3n ^{2} + 2**

However the correct answer is **3n ^{2} + 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(n ^{2})**

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 n^{2} times, o*i is calculated n^{2} times, the append function is called n^{2} times.

therefore n + n^{2} + n^{2} + n^{2} = 3n^{2} + n

add it to the rest and you get 3n^{2} + n + 2