Project Euler, problem 2: Determine the sum of the even numbers in the Fibonacci sequence up to 4 000 000. First I tried to
use a recursive algorithm for the sequence but I realized I dont have all the time in the world so I now use an iterative. It is still
extremely slow. Can I improve my code?
public class Euler2Correct { public static int Fibonacci(int j){ /** * Metod for returnerning number [I]j[/I] in the sequence. * */ if(j<=1){ return 1; } else if(j==2){ return 2; } int tmp; int a=2; int b=1; for(int k=3; k<=j; k++){ tmp=a+b; b=a; a=tmp; } return a; } public static void main(String[]args){ int s=0; for(int i=2; i<4000000; i=i+3){ //Every three number is even s = s + Fibonacci(i); } System.out.println(s); } }