﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectEuler.CSharp
{
    class Euler002
    {
        // Project Euler Problem #2: Even Fibonacci numbers
        // Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
        // 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
        // By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

        static void Main(string[] args)
        {
            int answer = GetSumOfFibonacciSeq(4000000);
            Console.WriteLine("Sum of the even-valued terms of fibonacci numeber which do not exceed four million is: {0}", answer);
            Console.ReadLine();
        }

        static int GetSumOfFibonacciSeq(int bound)
        {
            int lastNum = 0;
            int sum = 0;

            for (int i = 1; lastNum < bound; i++)
            {
                lastNum = Fibonacci(i);

                // only for even numbers
                if (lastNum % 2 == 0)
                {
                    sum += lastNum;
                }
            }

            return sum;
        }

        static int Fibonacci(int number)
        {
            if (number == 1)
            {
                return 1;
            }
            else if (number == 2)
            {
                return 2;
            }
            else
            {
                return Fibonacci(number - 2) + Fibonacci(number - 1);
            }
        }
    }
}
