偶数Fibonacci数的总和 [英] Sum of Even Fibonacci numbers

查看:105
本文介绍了偶数Fibonacci数的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我接受了来自 www.hackerrank.com 的挑战,计算了甚至斐波纳契数的总和。我已经提交了计算甚至Fibonacci数的逻辑,但我已经使用循环来计算总和。有没有任何数学逻辑可以计算偶数斐波那契数的总和而不需要循环。在此先感谢。

I got a challenge from www.hackerrank.com, calculate the sum of even Fibonacci number. I have submitted the logic that will calculate even Fibonacci numbers but i have used the loop to calculate the sum. Is there any mathematical logic that will calculate sum of even Fibonacci number without looping. Thanks in advance.

推荐答案

没有。

嗯,这不是真的,你可以把它变成一个递归算法,但......效率更低。



斐波那契数字是一个序列 - 每个术语取决于前两个术语的两个值。因此,为了评估F(n),你需要知道F(n-1)和F(n-2),两者都取决于两个早期的术语。



所以不,你不能在没有涉及循环的情况下神奇地找到F(n)的值。除非您使用该循环预先生成所有值并存储它们供您的应用程序访问 - 但由于序列是无限制的,您不会有空间...:笑:
No.
Well, that's not true, you could make it a recursive algorithm instead, but...that's even less efficient.

Fibonacci numbers are a sequence - each term depends on the two value of the two previous terms. So in order to evaluate F(n), you need to know both F(n-1) and F(n-2), both of which depend on two earlier terms.

So no, you can't magically find the value of F(n) without a loop being involved. Unless you use that loop to generate all the values in advance and store them for your app to access - but since the sequence is unbounded, you don;t have space for that...:laugh:


由于 Fibonacci 系列很容易通过循环生成,如果有其他算法( simplex sigillum veri ),我甚至都不会打扰。但是你可以试试递归。遗憾的是C#编程语言不支持尾递归优化 ...哎呀,等等...... C#中的尾递归 [ ^ ]。
Since the Fibonacci series is easily generated with a loop, I wouldn't even bother if there are alternate algorithms (simplex sigillum veri). However you might try recursion. It is a pity the C# programming language doesn't support tail recursion optimization...Oops, wait... "Tail recursion in C#"[^].


这篇关于偶数Fibonacci数的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆