Java Reference
In-Depth Information
The question Fibonacci originally raised was:
“How many pairs of rabbits will there be in the following months?”
Obviously at first, we put a single pair of newly born rabbits and have the
first month F 1 =1(
terminal state). Then we need to wait for another
month for these rabbits to become mature so that F 2 =1(
terminal state).
After which, the number of rabbits F i at month i is the number of rabbits at
month i
1 plus the number of rabbits newly generated by mature rabbits.
The number of mature rabbits at month i is precisely F i− 2 . Thus comes the
following recurrence relationship: F i = F i− 1 + F i− 2 with F 1 = F 2 = 1 (terminal
states).
Program 3.12 Displaying Fibonacci sequences using recursion
class FibonacciSequence {
public static int Fibonacci( int n)
{ if (n < =1) return 1;
else
return Fibonacci(n
1)+Fibonacci(n
2) ;
}
public static void main( String [ ]
args )
{
int i;
for (i=0;i < =30; i++)
System . out . print ( Fibonacci ( i )+ "" );
}
}
Running the above program, we get the first 31 Fibonacci numbers: 11235
8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121 393 196418 317811 514229 832040
1346269
3.5.3 Logarithmic mean
The logarithmic mean is often used in engineering to measure temperature
differences. There are several non-equivalent definitions proposed in the
literature. Here, we consider for our programming purpose the following
recursive definition:
1) L ( x 2 , ..., x n )
L ( x 1 , ..., x n− 1 )
log x n
L ( x 1 , ..., x n )=( n
,
log x 1
with L ( x )= x being the terminal case. Hence, for two elements x and y ,we
have the logarithmic mean expressed as
x
y
L ( x, y )=
log y .
log x
 
 
Search WWH ::




Custom Search