计算f(i)的函数 [英] Calculate the function of f(i)
问题描述
f(0)当我是一个正整数时,有一个函数f(i)满足以下条件。 )= 0,
$ p $因此,基于上述,我想编写一个程序来确定f(i)。
f(1)= 1
f(i)= f(i-1)+ f(i-2)
并编写一个程序来确定f(1000)。
解决方案以下Python 3.0脚本可以工作: p>
def f(i):
a,b = 0,1
(i):
a,b = b,a + b
返回a
print(f(0))
print(f(1 ))
print(f(2))
print(f(3))
print(f(1000))
给你:
0
1
1
2
和
< blockquote>
43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
When i is a positive integer, there is a function f(i) that satisfy the following.
f(0) = 0, f(1) = 1 f(i) = f(i-1) + f(i-2)
So, based on the above, I want to write a program to determine f(i). And write a program to determine f(1000).
解决方案The following Python 3.0 script will work:
def f(i): a, b = 0, 1 for i in range(i): a, b = b, a + b return a print(f(0)) print(f(1)) print(f(2)) print(f(3)) print(f(1000))
Giving you:
0 1 1 2
and
43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
这篇关于计算f(i)的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!