go to previous page   go to home page   go to next page

Answer:

Yes.


Complete Program

class FibonacciCalc
{
  public int fib( int n )
  {
    if ( n == 1 ) 
      return 1;
    else if ( n == 2 ) 
      return 1;
    else
      return fib( n-1 ) + fib( n-2 );
  }
}

class FibonacciTester
{
  public static void main ( String[] args)
  {
    int argument = Integer.parseInt( args[0] );  // Get N from the command line.
    
    FibonacciCalc f = new FibonacciCalc();
    int result = f.fib( argument );
    System.out.println("fib(" + argument + ") is " + result );
  }
}

When fib(n) is computed recursively, very many activations happen. Sometimes the time it takes to compute fib(n) is used as a benchmark, a program that tests the speed of a computer. Here is a bare-minimum program for fib(n):

Run the program with an argument on the command line, like java FibonacciTester 20 Here are some results of running the program on my 1997, 150MHz IBM ThinkPad 380ED. You might wish to run the program on your computer and compare speeds.

It takes a few seconds for the Java system to load and start running. This time is included in these measurements.


N102030354045
fib(N)55676583204092274651023341551134903170
time (sec) 223430360

QUESTION 21:

Is an iterative version of Fibonacci possible?


go to previous page   go to home page   go to next page