Consider a recursive function, called f, that computes powers of 3 using only the + operator. Assume n > = 0. int f(int n) { if (n == 0) return 1; return f(n-1) + f(n-1) + f(n-1); } Give an optimized version of f, called g, where we save the result of the recursive call to a temporary variable t, then return t+t+t. i got int g(int n) { if (n == 0) return 1; int t = g(n - 1); return t+t+t; } so now Write a recurrence relation for T(n), the number addition operations performed by g(n) in terms of n.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
icon
Related questions
Question
Consider a recursive function, called f, that computes powers of 3 using only the + operator. Assume
n > = 0. int f(int n) { if (n == 0) return 1; return f(n-1) + f(n-1) + f(n-1); } Give an optimized
version of f, called g, where we save the result of the recursive call to a temporary variable t, then
return t+t+t. i got int g(int n) { if (n == 0) return 1; int t = g(n - 1); return t+t+t; } so now
Write a recurrence relation for T(n), the number addition operations performed by g(n) in terms of n.
Transcribed Image Text:Consider a recursive function, called f, that computes powers of 3 using only the + operator. Assume n > = 0. int f(int n) { if (n == 0) return 1; return f(n-1) + f(n-1) + f(n-1); } Give an optimized version of f, called g, where we save the result of the recursive call to a temporary variable t, then return t+t+t. i got int g(int n) { if (n == 0) return 1; int t = g(n - 1); return t+t+t; } so now Write a recurrence relation for T(n), the number addition operations performed by g(n) in terms of n.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning