Please have a look at the following code
package Euler;
import java.util.ArrayList;
import java.util.List;
public class Problem2
{
public static void main(String[]args)
{
int firstNumber=1;
int secondNumber=2;
int thirdNumber = 0;
int sum = 0;
List array = new ArrayList();
while(true)
{
if(thirdNumber>=400000)
{
break;
}
else
{
thirdNumber = firstNumber+secondNumber;
System.out.println(thirdNumber);
if(thirdNumber%2==0)
{
array.add(thirdNumber);
}
firstNumber = secondNumber;
secondNumber = thirdNumber;
}
}
for( int i=0;i<array.size();i++)
{
int num = Integer.parseInt(array.get(i).toString());
sum = sum+num;
}
System.out.println("The Sum is: "+sum);
}
}
Here I am trying to solve this problem, from Project Euler, Here is the problem anyway.
Each new term in the Fibonacci sequence is generated by adding the
previous two terms. By starting with 1 and 2, the first 10 terms will
be:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not
exceed four million, find the sum of the even-valued terms.
This was the answer I got- 257112;
It says the answer is wrong. I don’t know why that is. For your information, my native language is not English, and I didn’t do math in English too. So I doubt whether I misunderstood the question.
Please help me to find the correct way to solve this. Thanks
One problem is here:
That’s not four million. You need one more zero.