Fig10_40 - int nextToLast = 1 int answer = 1 for int i = 2...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
public class Fig10_40 { /* START: Fig10_40.txt */ /** * Compute Fibonacci numbers as described in Chapter 1. */ public static int fib( int n ) { if( n <= 1 ) return 1; else return fib( n - 1 ) + fib( n - 2 ); } /* END */ / /* START: Fig10_41.txt */ /** * Compute Fibonacci numbers as described in Chapter 1. */ public static int fibonacci( int n ) { if( n <= 1 ) return 1; int last = 1;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int nextToLast = 1; int answer = 1; for( int i = 2; i <= n; i++ ) { answer = last + nextToLast; nextToLast = last; last = answer; } return answer; } /* END */ / public static void main( String [ ] args ) { System.out.println( "fib( 7 ) = " + fib( 7 ) ); System.out.println( "fibonacci( 7 ) = " + fibonacci( 7 ) ); } }...
View Full Document

This document was uploaded on 06/13/2011.

Ask a homework question - tutors are online