Fund. CS II (CS152 2004F)

Homework 22: Fibonacci Computation, Revisited

Due: Monday, 4 October 2004

Here is some simple code for computing the nth Fibonacci number.

public long fib(int n)
   long[] values = new long[n+1];
   values[0] = 0;
   values[1] = 1;
   for (int i = 2; i <= n; i++)
      values[i] = values[i-1] + values[i-2];
   } // for(int)
   return values[n];
} // fib(int)

Unfortunately, we run out of longs fairly quickly. Hence, we would benefit from returning a BigInteger and doing our intermediate computations with BigIntegers. Rewrite the code to do just that.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed Dec 8 10:36:55 2004.
The source to the document was last modified on Fri Oct 1 10:16:17 2004.
This document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,