Soldato
I am running through the problems on Project Euler and, rather stupidly, got stuck on problem 2!
The way I am reading it is basically the sum of all the even numbers in the fibbonaci sequence under 4000000. My program runs fine, but the answer is wrong. Any pointers? Not answers please, just an idea where I went wrong![Big Grin :D :D](/styles/default/xenforo/vbSmilies/Normal/biggrin.gif)
The way I am reading it is basically the sum of all the even numbers in the fibbonaci sequence under 4000000. My program runs fine, but the answer is wrong. Any pointers? Not answers please, just an idea where I went wrong
![Big Grin :D :D](/styles/default/xenforo/vbSmilies/Normal/biggrin.gif)
Code:
package euler_2;
/**
* By considering the terms in the Fibonacci sequence whose values do not
* exceed four million, find the sum of the even-valued terms.
*
* @author Woden
*/
public class Euler_2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int max = 4000000; // Upper limit of the exercise
int prev = 1; // Previous number int he sequence
int cur = 2; // Current number in the sequence
int next = 3; // next number in the sequence
int sum = 0; // Sum of even terms
while(prev + cur <= max) {
if(cur % 2 == 0) {
sum = sum + cur;
}
prev = cur;
cur = next;
next = prev + cur;
}
System.out.println(sum);
}
}