0

I am practicing questions in which I need to determine the big oh time complexity for a given piece of code. I have attempted the first 3 questions. Could anyone please confirm if the answers are correct?

Also, I am not sure how to calculat the big oh time complexity of the code in the 4th question. Could someone please explain how to calculate it?

Thank you

1:

sum, i = 0, 0 
while i**2 < n:
    sum = sum + i 
    i += 2

O(n^(1/2))

2.

i, j, sum =1,1,0 
while i < n**3:
    while j < n: 
        sum=sum+ i
        j += 1 
    i=i+n

O(n^3)

3.

i, sum = 0, 0 
while i *2 < n:
    sum = sum + i 
    i=i+1

O(log(n))

4.

i, sum =0,0 

while i < 3 * n:
    if i %2 == 0:
        j = 1
        while j < n:
            sum = sum + j
            j=j*2 
    else :
        j = 1
        while j < n:
            sum = sum * j
            j += (n // 5) 
    i=i+5
Dean
  • 1

0 Answers0