Question

Given the following two functions: def f(n): if n <= 0: return 0 return 1 +...

Given the following two functions:

def f(n):
   if n <= 0:
      return 0
   return 1 + f(n - 1)
def g(n):
   summ = 0
   for i in range(0, n, 1):
      summ += 1
   return summ
  1. (2 pts) State the runtime complexity of both f() and g()
  2. (2 pts) State the memory (space) complexity for both f() and g()
  3. (6 pts) Write another function called h(n) that does the same thing, but is significantly faster.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
runtime complexity of both f() is O(n)
runtime complexity of both g() is O(n)

space complexity of both f() is O(n)
space complexity of both g() is O(1)

another function called h(n) that does the same thing is
def h(n):
    return n
    

runtime complexity of both f() is O(1)
space complexity of both f() is O(1)

Add a comment
Know the answer?
Add Answer to:
Given the following two functions: def f(n): if n <= 0: return 0 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
  • 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

  • Question 18 CLO3 Analyze the following code and answer the questions that follow def F(n): If n <= 1: return n else: return F(n-1)+F(n-2) for i in range (n) print (F(i)) Result: 0 1 1 2 3 5 8...

    Question 18 CLO3 Analyze the following code and answer the questions that follow def F(n): If n <= 1: return n else: return F(n-1)+F(n-2) for i in range (n) print (F(i)) Result: 0 1 1 2 3 5 8 13 a. Write number of operations as a function when the code is execute b If n 7, what is the total number of operations? c. What is the complexity of the algorithm behind the code? (2 Marks) (2 Marks) (1...

  • IN PYTHON Functions f(), g(), and h() are defined as follows: def f(x):     return 2*g(x)...

    IN PYTHON Functions f(), g(), and h() are defined as follows: def f(x):     return 2*g(x) def g(x):     return h(x)**2 def h(x):     return x//2 Consider the execution of function call f(2). Show the state of the program stack just prior to executing statement return x//2.

  • def ged(m,n) : if m n == 0: return n else: return god (n, møn) Without...

    def ged(m,n) : if m n == 0: return n else: return god (n, møn) Without typing the code into Python, what are the values of the input arguments of the second recursive call if the function gcd is called from the main program as gcd(537,44)? (Hint: do not count the initial call to the function from the main program) Om = 44, n = 9 Om = 44, n = n = 537 Om = 9, n = 8...

  • 1. (10 pts) For each of the following pairs of functions, indicate whether f = 0(g),...

    1. (10 pts) For each of the following pairs of functions, indicate whether f = 0(g), f = Ω(g), or both (in which case f-6(1). You do not need to explain your answer. f(n) (n) a) n (b) n-1n+1 (c) 1000n 0.01n2 (d) 10n2 n (lg n)2 21 е) n (f) 3" (g) 4" rl. 72 i-0 2. (12 pts) Sort the following functions by increasing order of growth. For every pair of consecutive functions f(n) and g(n) in the...

  • ###############recursion (Python) ##def facto( num ): ## if num == 0: ## return 1 ## else:...

    ###############recursion (Python) ##def facto( num ): ## if num == 0: ## return 1 ## else: ## return num * facto( num - 1 ) ## ##Task 2. N! = 1*2*3*...*(N-1)*N as we know. There is another "double factorial": ##N!! : ##1)if N is odd then N!! = 1*3*5*7*9*...*N ##2)if N is even then N!! = 2*4*6*8*...*N ## ####Task 2. Create a function facto2 which calculates facto2(N) = N!! ##Show that your function works. ######use input(...)

  • I need help for the order of growth for functions in Python 3. Q1: What is...

    I need help for the order of growth for functions in Python 3. Q1: What is the order of growth for the following functions? Kinds of Growth Here are some common orders of growth, ranked from no growth to fastest growth: 1. Θ(1) — constant time takes the same amount of time regardless of input size 2. Θ(log n) — logarithmic time 3. Θ(n) — linear time 4. Θ(n log n) — linearithmic time 5. Θ(n2 ) 6. Θ(n3 ),...

  • long fib(int n) { long result;    if (n==0) result = 0; else if (n==1 ||...

    long fib(int n) { long result;    if (n==0) result = 0; else if (n==1 || n==2) result = 1; else result = fib(n-1) + fib(n-2);    return result; } [C language] Question : Why is this code is so slow? Explain ex2) Make the program from exercise 1 faster by trading space for time. Create an array of int that can be used as a cache. Each index in the array should correspond to a value that you can...

  • 2. Give the asymptotic running time of each the following functions in e notation. That is,...

    2. Give the asymptotic running time of each the following functions in e notation. That is, write down a recurrence relation for each recursive function below and solve it. Show your work def Pow(x, n): 2 if n-0: 3 return 1 end 5 e f Pow(x, [n/2]) 1 # n is even if n % 2-0: 9 return f f 10 end #nis odd 12return r*f*.f 13 end

  • Consider the following Python program, where x is assumed to be a list of integers. def...

    Consider the following Python program, where x is assumed to be a list of integers. def mystery_func(x): n = len(x) for i in range(n - 1): for j in range(i + 1, n): if x[j] - x[i] < j - i: return False return True 3a. In plain English, describe what it accomplishes (i.e., the relationship between input and output, not how the code works). 3b. Analyze the running time and express it with big O. 3c. Rewrite this function...

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