//ProjectEuler/Java/Problem2.java //Matthew Ellison // Created: 03-01-19 //Modified: 03-28-19 //The sum of the even Fibonacci numbers less than 4,000,000 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses /* Copyright (C) 2019 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ import mattrixwv.Stopwatch; import mattrixwv.Algorithms; import java.util.ArrayList; public class Problem2{ private static final int TOP_NUM = 3999999; public static void main(String[] argv){ Stopwatch timer = new Stopwatch(); timer.start(); //Get a list of all fibonacci numbers < 4,000,000 ArrayList fibNums = Algorithms.getAllFib(TOP_NUM); Integer sum = 0; //Step through very element in the list checking if it is even for(Integer num : fibNums){ //If the number is even add it to the running tally if((num % 2) == 0){ sum += num; } } timer.stop(); //Print the results System.out.printf("The sum of all even fibonacci numbers <= %d is %d\n", TOP_NUM, sum); System.out.printf("It took %s to run this algorithm\n", timer.getStr()); } } /* Results: The sum of all even fibonacci numbers <= 3999999 is 4613732 It took 940.825 microseconds to run this algorithm */