Project Euler – Problem 2 Solution (Java)
http://projecteuler.net/index.php?section=problems&id=2
/* Project Euler - Problem 2 (http://projecteuler.net/index.php?section=problems&id=2)
*
* I did realize that every 3rd Fibonacci number is even, but I haven't quite come
* up with a way to utilize that knowledge to simplify the loop. I'll keep thinking
* about it and see if I come up with something better.
*/
public static void p2(){
int sum = 0;
int fib1 = 1;
int fib2 = 1;
int temp;
do{
temp = fib1 + fib2;
fib1 = fib2;
fib2 = temp;
if(fib2 % 2 == 0) sum = sum+fib2;
} while(fib2<4000000);
System.out.println(sum);
}
Tina on January 28th 2009 in Project Euler