首页 > 其他 > 详细

我的欧拉工程之路_2

时间:2014-04-06 05:18:15      阅读:491      评论:0      收藏:0      [点我收藏+]

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);
    }
}


本文出自 “迁客的小屋” 博客,请务必保留此出处http://qianke.blog.51cto.com/5616176/1390973

我的欧拉工程之路_2,布布扣,bubuko.com

我的欧拉工程之路_2

原文:http://qianke.blog.51cto.com/5616176/1390973

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!