免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2033 | 回复: 1

[其他] Project Euler - 002 [复制链接]

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
发表于 2015-09-25 09:41 |显示全部楼层
本帖最后由 icymirror 于 2015-09-25 09:42 编辑

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开始时,开始10项是:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ......
现在,把此数列中的数值不超过4000000的项中的偶数求和,结果是多少?

代码:
  1. package main

  2. import (
  3.         "fmt"
  4. )

  5. func Problem002(scope int) int {
  6.         sum := 0

  7.         first := 1
  8.         second := 2
  9.        
  10.         for {
  11.                 if (second > scope) {
  12.                         break
  13.                 }
  14.                
  15.                 sum = sum + second

  16.                 first, second = first + second * 2, first * 2 + second * 3
  17.         }
  18.        
  19.         return sum
  20. }

  21. func main() {
  22.         fmt.Println("Problem 002 result: ", Problem002(4000000))
  23. }
复制代码

论坛徽章:
0
发表于 2015-10-09 14:18 |显示全部楼层
拿来练练手。
4613732
  1. #!perl6
  2. my $n = 4000000;
  3. say [+] (1, 2, *+* ...^ * > $n).grep: * %% 2
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP