斐波那契数列之和
我在此处找到了此任务。
给定第 i 个 (1<=i<=35) 斐波那契数 number F(i) 计算总和 第 i 到第 i+9 个数字 F(i)+F(i+1)+...+F(i+9) 以及最后一个 第 i+246 位 F(i+246)
我一直在尝试使用 python 和一些技巧(Binnet 的公式和一个棘手的递归)来解决这个问题:
f=lambda n:((1+5**.5)**n-(1-5**.5)**n)/(2**n*5**.5)
exec"n=input();print int(55*f(n)+88*f(n+1)+f(n+6)%10);"*input()
但我还没有设法挤压思想给定的源代码限制是 111,我的是 115,有什么提示如何改进我的解决方案吗?
我是 python 的新手,因此任何能够带来成功解决方案的帮助将不胜感激。
谢谢,
I found this task here.
Given the ith (1<=i<=35) Fibonacci
number F(i) calculate the sum of the
ith till i+9th number
F(i)+F(i+1)+...+F(i+9) and the last
digit of the i+246th one F(i+246)
I have been trying to solve this using python and some tricks(Binnet's formula and a tricky recurrence):
f=lambda n:((1+5**.5)**n-(1-5**.5)**n)/(2**n*5**.5)
exec"n=input();print int(55*f(n)+88*f(n+1)+f(n+6)%10);"*input()
but I didn't yet managed to squeeze thought the give source code limit which is 111 and mine is 115,any hints how to improve my solution?
I am a rather newbie to python so any sort of help resulting in a successful solution will be much appreciated.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(6)
这是 110 的解决方案,我不得不重写公式并使用 @Gareth 的建议:
p=5**.5
f=lambda n:((1+p)**n-(1-p)**n)/(2**n*p)
exec "n=input();print int(f(n+11)-f(n+1)+f(n+6)%10);"*input()
保存另一个符号,现在 109 (使用 n
进行操作并摆脱 +11
) :
p=5**.5
f=lambda n:((1+p)**n-(1-p)**n)/(2**n*p)
exec "n=input()+6;print int(f(n+5)-f(n-5)+f(n)%10);"*input()
编辑:计算特定数字的新方法,保存另外 4 个符号并允许避免 int()
:
def f(n):exec"a=b=1;"+"a,b=b,a+b;"*(n-1);return a
exec "n=input()+6;print f(n+5)-f(n-5)+f(n)%10;"*input()
抱歉,我在发帖之前没有正确阅读您的问题。我很高兴你至少发现了它的一些用处。
我不懂 Python,但在 Mathematica 中,尽可能通用:
f[1] = 1;
f[2] = 1;
f[x_] := f[x] = f[x - 1] + f[x - 2]
t = 0;
n = 35;
For[i = 0, i <= 9, i++, t += f[n + i]]
t += f[n + 246] ~Mod~ 10
或者,在简洁的 Mathematica 中,仍然不使用 Fibonacci 函数:
f[1|2]=1;a:f@x_:=a=f[x-1]+f[x-2];Sum[f[#+x],{x,0,9}]+f[#+246]~Mod~10&
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您尝试过使用这个求和公式吗?
http://en.wikipedia.org/wiki/Fibonacci_number#Second_identity(“第二身份”)?
Did you try to use this sum formula?
http://en.wikipedia.org/wiki/Fibonacci_number#Second_identity ("Second Identity")?