博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Project Euler Problem 2: Even Fibonacci numbers
阅读量:7212 次
发布时间:2019-06-29

本文共 840 字,大约阅读时间需要 2 分钟。

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.

C++:

#include 
using namespace std;const long N = 4000000;int main(){ long f1 = 1, f2 = 2, f3; long ans = f2; for(;;) { f3 = f1 + f2; if(f3 > N) break; if(f3 % 2 == 0) { ans += f3; } f1 = f2; f2 = f3; } cout << ans << endl; return 0;}

Python:

ls=[1,1]ans=0while ls[-1] <4000000:	ls.append(ls[-1]+ls[-2])	if (ls[-1]&1) == 0:		ans+=ls[-1]print(ans)
Run results:
4613732

转载于:https://www.cnblogs.com/tigerisland/p/7564046.html

你可能感兴趣的文章
我的友情链接
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
jconsole监控weblogic 10.3
查看>>
discuz 抱歉,Email 包含不可使用的邮箱域名
查看>>
java的动态代理机制
查看>>
Windows Server 2012 DNS
查看>>
我的友情链接
查看>>
记录一次最新版MySQL-server-5.6.20-1.el6.x86_64.rpm的安装
查看>>
我的友情链接
查看>>
codevs——1576 最长严格上升子序列(序列DP)
查看>>
迷你Wifi摄像机介绍
查看>>
centos7 nfs挂载设置
查看>>
Apache的MPM
查看>>
jenkins搭建和简单发布代码(待续)
查看>>
前端必备的浏览器知识(渲染过程、回流和重绘等)
查看>>
网络虚拟化技术 TUN/TAP MACVLAN MACVTAP
查看>>
内存理解
查看>>
IBM AIX 平台下常用命令
查看>>
linux 路由表解析
查看>>