Computing Powers
This problem is a case study in using recursive thinking to improve the efficiency of an iterative algorithm. You will write a sequence of methods for the exponentiation of floating point numbers. Parts I and II involve writing rather simple-minded iterative and recursive methods for the task. In Parts III and IV you use more sophisticated recurrence relations in order to write more efficient recursive methods. In Part V you convert your method from Part IV into an iterative method that is far more efficient than the Part I iterative method.
You may put all of your code for this project into a single class file – PowersUsername.java. All of your
methods should be static.
Part I:
Write an iterative method called power1 to compute bn
, where b is of type double and n is an integer ≥ 0.
Use a simple for-loop that repeatedly (n times) multiplies an accumulator variable by b.
Part II:
Write a recursive method power2 that accomplishes the same task as power1, but is based on the
following recurrence relation:
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
23 | 24 | 25 | 26 | 27 | 28 | 1 |
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 | 1 | 2 | 3 | 4 | 5 |
Get Free Quote!
340 Experts Online