Question

In Python State g(n)'s runtime complexity: def f(n): if n <= 1:` return 1 return 1...

In Python

State g(n)'s runtime complexity:

def f(n):
   if n <= 1:`
      return 1
   return 1 + f(n/2)

def g(n):
   i = 1
   while i < n:
       f(i)
       i *= 2
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Recursive function for f is
T(n) = T(n/2) + 1
     = T(n/4) + 1 + 1
     = T(n/8) + 1 + 1 + 1
     ......
     ......
     ......
     = T(n/n) + 1 + .... + 1 + 1 + 1 [log(n) +1 terms]
     = T(1) + 1 + .... + 1 + 1 + 1 [log(n) +1 terms]
     = 10 + 1 + .... + 1 + 1 + 1 [log(n) +1 terms]
     = clog(n)
     (Where c is a constant)
     = O(logn)
So, Time complexity of function f() = O(logn)

========================================

Time complexity of g(n):
Number of values for i are log(n)
So, The while loop in g() is running for log(n) times.
Each time in the while loop it is making call to function f()

So, Total time complexity of g(n) = O(log(n) * log(n))

Add a comment
Know the answer?
Add Answer to:
In Python State g(n)'s runtime complexity: def f(n): if n <= 1:` return 1 return 1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT