package Fibonacci2; /** * Slow Fibonacci Number Calculator. * * @author Wayne Eberly */ public class SFibonacci { /** * Computes a specified Fibonacci number as a long integer by a direct * application of the recursive definition. * *

* This method is too slow to be used in practice. It is provided * to facilitate testing of more efficient methods. *

* *

* Precondition: * i is a nonnegative integer.
* Postcondition: * Value returned is the ith * Fibonacci number. *

* * @param i Index of the Fibonacci number to be calculated * @return The ith Fibonacci number * @throws IllegalArgumentException If the input integer is negative * */ protected static long fib(int i) { if (i<0) { throw new IllegalArgumentException("Negative Input: " + i); } else { // i >= 0 if (i==0) { return 0; } else { // i >= 1 if (i==1) { return 1; } else { // i >= 2 return fib(i-1) + fib(i-2); } } } } }