|
|
|
|
||
|
|
Recursion and Iteration |
|
||
|
|
Introduction This is another exercise to provide an example of the use of recursion. Use the following time algorithm to compare a recursive method with an iterative method. |
|
||
|
|
long time; time = System.currentTimeMillis(); System.out.print(“System
time is milliseconds “); System.out.println(time); |
|
||
|
|
Programming Assignment Write two programs. Each will calculate the constant e or Euler’s constant which is given as: e = 1 + 1/1! + 1/2! + 1/3! +…+ 1/n! Write the programs to run the calculation to ten decimal places. One program will use an iterative method to do the calculation and one program will use a recursive method to do the calculation. Compare the times of the two algorithms. Why do you suppose that they are so different? |
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|