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.
斐波纳契数列中每个数都是通过其前两项相加获得的.从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); } }
本文出自 “迁客的小屋” 博客,请务必保留此出处http://qianke.blog.51cto.com/5616176/1390973
原文:http://qianke.blog.51cto.com/5616176/1390973