3. The following program computes 2: int power2(int n) { } if (n= 0) return 1;...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
3. The following program computes 2": int power2(int n) { } if (n= 0) return 1; return power2(n-1)+power2(n-1); (a) Find a recurrence formula as we learned in class. Find the runtime. What is the big problem with this function? (hint: We discussed something similar in class). (b) Introduce a small modification that makes the function run in linear time. Show why the runtime is linear. (c) (bonus) The following function also calculates 2": int power2New (int n) { } if (n= 0) return 1; if (n % 2 == 0) { } int result = power2New (n/2); return result*result; else return 2*power2New (n-1); Explanation: If n is even, then 2" = (2)2, so we can calculate 2 recursively and square, cutting half of n in one move. Otherwise, we resort to the previous method. Show that the runtime of power2New is logarithmic in n. Hint: It's easy to show it when n is even, but sometimes n is odd... The trick is to show that the entire function is logarithmic nonetheless. 3. The following program computes 2": int power2(int n) { } if (n= 0) return 1; return power2(n-1)+power2(n-1); (a) Find a recurrence formula as we learned in class. Find the runtime. What is the big problem with this function? (hint: We discussed something similar in class). (b) Introduce a small modification that makes the function run in linear time. Show why the runtime is linear. (c) (bonus) The following function also calculates 2": int power2New (int n) { } if (n= 0) return 1; if (n % 2 == 0) { } int result = power2New (n/2); return result*result; else return 2*power2New (n-1); Explanation: If n is even, then 2" = (2)2, so we can calculate 2 recursively and square, cutting half of n in one move. Otherwise, we resort to the previous method. Show that the runtime of power2New is logarithmic in n. Hint: It's easy to show it when n is even, but sometimes n is odd... The trick is to show that the entire function is logarithmic nonetheless.
Expert Answer:
Answer rating: 100% (QA)
a The recurrence formula for the original power2 function is Tn 2Tn1 1 where Tn represents the time ... View the full answer
Related Book For
Introduction To Programming In Java An Interdisciplinary Approach
ISBN: 9780672337840
2nd Edition
Authors: Robert Sedgewick, Kevin Wayne
Posted Date:
Students also viewed these programming questions
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
"internet radios" for streaming audio, and personal video recorders and players. Describe design and evaluation processes that could be used by a start-up company to improve the usability of such...
-
The current zero-coupon yield curve of semi-annually compounded rates for risk-free bonds is as follows: 1.0 Years 1.5 Years 2.0 Years 2.5 Years 3.0 Years 9.00% 10.20% 3.00% 4.00% 6.20% MATURITY 0.5...
-
Find the value x for which: a. P ( 220 x) = 0.005 b. P( 220 x) = 0.01 c. P( 220 < x) = 0.005 d. P( 220 < x) = 0.01
-
FIGURE EX10.28 shows the potential energy of a 500 g particle as it moves along the x-axis. Suppose the particle??s mechanical energy is 12 J. a. Where are the particle??s turning points?b. What is...
-
Allan and Koraev both owned condominiums in the same building. Koraevs unit was directly above Allans. While Allan lived in her own unit, Koraev leased his. The leasing of Koraevs unit was managed by...
-
Given the following project to landscape a new building site, (a) Draw a Gantt chart using MSP. (b) Find the critical path and project duration in days. (c) Given that each resource is assigned 100%...
-
Discuss why people often resist new ideas such as the Relational Model in Business Logic ?
-
A progressive homogeneous plane wave containing z- at a frequency of f = 1MHz in seawater see for. (For sea water at 1MHz take Find the wave propagation parameters. b) Determine the time dependent...
-
Consider your current job or a job you recently held. What types of training did you receive for the job? What types of training would you like to receive? Why? Was the company sponsored training...
-
1.make and design an E-Safety an ICT wall display that you will share to a group of learners within the class you are supporting 2. How you sourced and used the ICT resources correctly and safely,...
-
Please explain this assignment, and give me some suggestions Objective The overall aim of Assignment 3 is to develop two items that address an identified opportunity or challenge: a prototype of a...
-
4. Using a geometric series, write 0.38 fraction). = 0.38383838... as a rational number (i.e. a proper 3 marks 5. Using an appropriate combination C, what is the coefficient of a in the expansion of...
-
Worcester Wallpaper Incorporated manufactures quality wallpaper. They are considering buying the glue for the back of the wallpaper rather than producing it. The costs to manufacture the glue for...
-
Man dies of massive heart attack after being sent home from ED A seventy-three-year-old man and his wife walked into the ED at 3:00 a.m. He told the nurse at the desk that he was having chest pain...
-
A company pledges their receivables so they may Multiple Choice Charge a factoring fee. Increase sales. Recognize a sale. Collect a pledge fee. Borrow money. Failure by a promissory notes' maker to...
-
The indegrees and outdegrees of pages in the web obey a power law that can be modeled by a preferred attachment process. Suppose that each web page has exactly one outgoing link. Each page is created...
-
How many different graphs are there with \(V\) given vertices?
-
Write a client UniverseTrace that produces traces of the \(n\)-body simulation system like the static images. 100 steps 150 steps 1.000 steps 10,000 steps 150 steps 880 steps 1,600 steps 3,100 steps...
-
A national news organization developed the graphic shown in Figure 22 to illustrate the change in the highest marginal tax rate effective January 1, 2013. Why might this graph be considered...
-
A home security company located in Minneapolis, Minnesota, develops a summer ad campaign with the slogan When you leave for vacation, burglars leave for work. According to the city of Minneapolis,...
-
Soccer continues to grow in popularity as a sport in the United States. High-profile players such as Hope Solo and Landon Donovan have helped generate renewed interest in the sport at various age...
Study smarter with the SolutionInn App