Initial commit

This commit is contained in:
Michael Reber
2019-11-15 12:59:38 +01:00
parent 40a414d210
commit b880c3ccde
6814 changed files with 379441 additions and 0 deletions
@@ -0,0 +1,45 @@
Fig10_40.cpp - Algorithms to compute Fibonacci numbers
#include <iostream.h>
/* START: Fig10_40.txt */
/**
* Compute Fibonacci numbers as described in Chapter 1.
*/
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.
*/
int fibonacci( int n )
{
if( n <= 1 )
return 1;
int last = 1;
int nextToLast = 1;
int answer = 1;
for( int i = 2; i <= n; i++ )
{
answer = last + nextToLast;
nextToLast = last;
last = answer;
}
return answer;
}
/* END */
int main( )
{
cout << "fib( 7 ) = " << fib( 7 ) << endl;
cout << "fibonacci( 7 ) = " << fibonacci( 7 ) << endl;
return 0;
}