This program calculates the nth term of the Fibonacci series using a recursive algorithm with memoization. It also measures the time spent on the calculation and reports the result.
- Student Name: Dmytro Poliak
- Compile the program using a Java compiler.
- Run the compiled program.
- Enter a non-negative integer
nwhen prompted. - The program will calculate and display the Fibonacci result for the entered
n. - The time spent on the calculation will be printed in nanoseconds.
The program utilizes a recursive approach with memoization to calculate Fibonacci numbers efficiently. The result for each Fibonacci term is stored in an array (memo), and if the term has already been calculated, it is retrieved from the array, avoiding redundant computations.
The program measures the time spent on the calculation using System.nanoTime() and reports the result in nanoseconds.
- The program includes input validation, throwing an
IllegalArgumentExceptionif the enterednis less than 0.
Compile the program using a Java compiler (e.g., javac FibonacciCalculator.java) and run the compiled program (java FibonacciCalculator).
- The program supports a maximum value of
ndefined byMAX_N(currently set to 100) due to memoization constraints.
For larger Fibonacci terms, consider alternative algorithms, such as an iterative approach, for further performance improvement.