Monday, July 16, 2012

answer java programs for Eular projects-problem2


PROBLEM
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.

ANSWER
4613732

JAVA CODE


public class Fibbonaci {
public static void main(String []args){
int pre=1;
int sum=0;
for (int i = 2; i <4000000; ) {
if(i%2==0)
sum+=i;
int tmp=pre;
pre=i;
i+=tmp;
}
System.out.println(sum);
}
}

0 comments:

Post a Comment