欧拉项目第二题

不知不觉,欧拉项目就零零碎碎的做了15道题了,觉得真是非常的花时间,但是依然还是乐此不彼。下面奉上欧拉项目第二题代码

 


  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, ... Find the sum of all the
   even-valued terms in the sequence which do not exceed four million.

 


  这道题算法不难,主要点在于审题。可能是我英语不太好吧,开始做了好久都没有做出来,我还以为是我算法出了问题,后来验证了很多遍都没有什么问题。然后仔细去看了下题目, even-valued是什么意思。一看,原来是偶数,顿时恍然大悟。简单的修改了一下代码,即可求得正解。代码如下:

 

public class Problem2 {
	
	public static void main(String[] args) {

		long a = 1;
		long b = 1, c;
		long sum = 0;

		while (b < 4000000) {
			b = a + b;
			a = b - a;
			if (a % 2 == 0) {
				sum += a;
			}

		}
		System.out.println(a);
		System.out.println(sum);

	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值