document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.math.BigDecimal;
  2. import java.math.BigInteger;
  3. import java.math.MathContext;
  4. import java.math.RoundingMode;
  5.  
  6. /*
  7.  ============================================================================
  8.  Author      : Catarina Moreira
  9.  Copyright   : Catarina Moreira all rights reserved
  10.  Description : Implementation of the fibonacci sequence using Binet\'s formula
  11.  ============================================================================
  12. */
  13.  
  14. public class Binet
  15. {
  16.     public BigInteger runFibonacci(int n)
  17.     {
  18.        if( n == 0)
  19.           return BigInteger.ZERO;
  20.    
  21.        if( n == 1)
  22.           return BigInteger.ONE;
  23.        
  24.        double sqrt_5 = Math.sqrt(5);
  25.        BigDecimal big_sqrt_5 = new BigDecimal( Double.toString( sqrt_5 ) );
  26.            
  27.        BigDecimal part1 = big_sqrt_5.add(BigDecimal.ONE);
  28.        BigDecimal part2 = BigDecimal.ONE.subtract(big_sqrt_5);
  29.        BigDecimal power2 = new BigDecimal("2");
  30.               power2 = power2.pow(n);
  31.            
  32.        BigDecimal result = (( part1.pow(n) ).subtract( part2.pow(n) )).divide( power2.multiply(big_sqrt_5) );
  33.        
  34.        return result.setScale(0, RoundingMode.FLOOR).toBigInteger();
  35.     }  
  36. }
');