The famous Fibonacci sequence is ubiquitous in art, nature, and Math. Here it is:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Note that the number at position n in the sequence is the sum of the numbers at positions n – 1 and n – 2. In other words, fib(n) is computed by two recursive calls to fib:
fib(0) = 0
fib(1) = 1
fib(n) = fib(n – 1) + fib(n – 2) if n > 1
Here's a test harness that tests a Fibonacci function:
Implement Fibonacci.j. This class contains a single static method called fib that uses the double recursion above to compute a result.