Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: Overflow and time-efficient in a for-loop?

1. Overflow and time-efficient in a for-loop?

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);

}
}```  Reply With Quote

3. Re: Overflow and time-efficient in a for-loop?

You could read up on dynamic programming if you want to get it much faster, but I am not going to explain it in this post, it would be a little bit too much.  Reply With Quote

4. Re: Overflow and time-efficient in a for-loop? Originally Posted by Cornix You could read up on dynamic programming if you want to get it much faster, but I am not going to explain it in this post, it would be a little bit too much.
I was more concerned about obvious flaws and time-consuming operations(like the recursive way, although simpler). I went through the Wiki page and it looks interesting but I doubt that it requires that kind of knowledge. Just something easy and neat.  Reply With Quote

5. Re: Overflow and time-efficient in a for-loop?

Think about your algorithm or - even better - write down the calculations. Or just let the program do it for you by adding some printlns. Here's a result of doing just that - where each line represents the calculations done per call of the fibonacci method:

```2 + 1,3 + 2,5 + 3
2 + 1,3 + 2,5 + 3,8 + 5,13 + 8,21 + 13
2 + 1,3 + 2,5 + 3,8 + 5,13 + 8,21 + 13,34 + 21,55 + 34,89 + 55
2 + 1,3 + 2,5 + 3,8 + 5,13 + 8,21 + 13,34 + 21,55 + 34,89 + 55,144 + 89,233 + 144,377 + 233
2 + 1,3 + 2,5 + 3,8 + 5,13 + 8,21 + 13,34 + 21,55 + 34,89 + 55,144 + 89,233 + 144,377 + 233,610 + 377,987 + 610,1597 + 987```

See any calculations being repeated again and again?  Reply With Quote

6. Re: Overflow and time-efficient in a for-loop?

Yes, this was a good observation. So I better keep the running total in the Fibonacci method, and use the previous results to prevent doing all that extra work. I'll modify it.  Reply With Quote