document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package problems;
  2.  
  3. import java.math.BigInteger;
  4.  
  5. /*
  6.  ============================================================================
  7.  Name        : Problem1.java
  8.  Author      : Catarina Moreira
  9.  Copyright   : Catarina Moreira all rights reserved
  10.  Description : Project EULER problem 2: Even Fibonacci numbers
  11.  ============================================================================
  12. */
  13.  
  14. public class Problem2
  15. {
  16.    /*
  17.     * Each new term in the Fibonacci sequence is generated by adding the previous two terms.
  18.     * By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
  19.     *
  20.     * By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
  21.     *
  22.    */
  23.  
  24.    public int evenFibonacci(int n)
  25.    {
  26.       int result = 0;
  27.       for( int i = 0; ; i++ )
  28.       {
  29.          int fib = getFibonacci( i );
  30.            
  31.          if( fib > n )  break;
  32.    
  33.          if( fib % 2 == 0 )
  34.         result += fib;
  35.       }
  36.        
  37.       return result;
  38.    }
  39.    
  40.    public int getFibonacci( int n )
  41.    {
  42.       int a = 0, b = 1, c = 0;
  43.    
  44.       for(int i = 0; i < n; i++, a = b, b = c )
  45.          c = a + b;
  46.      
  47.       return c;
  48.    }   
  49. }
');