2

How exaclty do you determine this specific codes runtime is theta(n^2)??I can see theres two while loops which go from i and j to n but would like a more precise way of determing this? If someone could explain is simple terms as I am very new to this I would appreciate it.

def f(n):

i = 0

  while i < n:

   j = 0

     while j < n:

      j = j + 1

 i = i + 5
Raphael
  • 73,212
  • 30
  • 182
  • 400
78lee
  • 89
  • 1
  • 3

1 Answers1

0

Solution 01) Analysis what code does.

Your code execute the first loop n times.

And for each executions on first loop it will execute n times.

So the result will be n * n = n^2

Solution 02) Try to count how many times execute the instruction

def f(n):
count = 0
i = 0
  while i < n:
   j = 0
     while j < n:
      j = j + 1
      print("execute:" + count)
      count = count + 1
 i = i + 1