Even Fibonacci numbers
Problem 2
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.
斐波纳契数列
问题 2
斐波纳契数列中每个数都是通过其前两项相加获得的.从1和2开始,前十项如下:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
考虑斐波纳契数列中不超过400万的项, 其中是偶数的项的和是多少.
//此程序用于解决欧拉工程第2题
public class Euler2
{
public static void main(String[] args)
{
int sum=2;
for(int a=1,b=2,c;(c=a+b)<4000000;)
{
if(c%2==0)
sum+=c;
a=b;b=c;
}
System.out.println("sum="+sum);
}
}
转载于:https://blog.51cto.com/qianke/1390973