Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fibonacci;
- import java.math.BigInteger;
- /*
- ===========================================================================
- Author : Catarina Moreira
- Copyright : Catarina Moreira all rights reserved
- Description : Implementation of the fibonacci sequence using a fast recursive
- algorithm
- ===========================================================================
- */
- public class RecursiveFast extends Fibonacci
- {
- public BigInteger runFibonacci( int n )
- {
- if( n == 0)
- return BigInteger.ZERO;
- if( n == 1)
- return BigInteger.ONE;
- return auxFibonacci( n, BigInteger.ZERO, BigInteger.ONE );
- }
- public BigInteger auxFibonacci( int n, BigInteger val1, BigInteger val2 )
- {
- if( n == 1 )
- return val2;
- return auxFibonacci( n - 1, val2, val1.add( val2 ));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement