001package net.n1da.dev.euler;
002
003import net.n1da.dev.euler.core.*;
004import net.n1da.dev.euler.helper.*;
005
006/**
007 * Solution for problem 2
008 * "<strong>Even Fibonacci numbers</strong>"
009 * completed on Tue, 7 Apr 2015, 23:38.
010 * 
011 * @author <a href="mailto:nico@danneberg.de">Nico Danneberg</a>
012 * @since 07.04.2015 23:12:23
013 * 
014 * @see <a href="https://projecteuler.net/problem=2" target="_blank">Problem @ Project Euler</a>
015 * @see <a href="http://n1da.net/specials/projekt-euler/problem-2-gerade-fibonacci-zahlen/">Solution @ Ni-Da-Net</a>
016 */
017public class PE0002 extends Problem {
018
019        /**
020         * A simple constructor to set {@link Problem#number number}
021         * and {@link Problem#title title}.
022         */
023        public PE0002() {
024                super( 2, "Even Fibonacci numbers" );
025        }
026
027        /**
028         * Solving this problem by just adding the given pairs, check if the
029         * result is even and sum up the even sums.
030         * 
031         * @return the sum of all even part of the series
032         */
033        @Override
034        public String solve() {
035                int a = 1;
036                int b = 2;
037                int s = b;
038                
039                while( a+b < 4000000 ) {
040                        int c = a + b;
041                        
042                        if( c%2 == 0 )
043                                s += c;
044                        
045                        a = b;
046                        b = c;
047                }
048
049                return IO.i2s( s );
050        }
051
052}