Calculate the function of f(i) [closed]

感情迁移 提交于 2019-12-13 09:55:57

问题


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).


回答1:


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




回答2:


Since you seem to be leaving the programming language open, I choose python which is great for these things.

You can simply do:

def f(i):
    if i == 0:
        return 0
    elif i == 1:
        return 1
    return f(i-1)+f(i-2)

If you want to be more fancy and efficient, use iterators:

def f():
    a, b = 0, 1
    while True:            
        yield a            
        a, b = b, a + b

The code runs very fast:

for i, val in enumerate(f()):
    if i == 1000:
        print val
        break

and return your desired value f(1000), which is:

43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875

@DmitryBychenko: the value you returned is actually f(999).

@MartinEvans: your code is actually incorrect (off by 1). One obvious way to see it is that the values returned by f(1) and f(2) are wrong:

>>> def f(i):
...     a,b = 0, 1
...     for i in range(i-1):
...             a,b = b, a+b
...     return a
... 
>>> f(0)
0
>>> f(1)
0
>>> f(2)
1
>>> f(3)
1



回答3:


100th Fibonacci number is a huge value, so BigInteger (C#) or its analogue is required. C# implementation can be something like this (I doubt if it'll be accepted as homework code).

private static IEnumerable<BigInteger> fibo() {
  yield return 0;
  yield return 1;

  BigInteger first = 0;
  BigInteger second = 1;

  while (true) {
    BigInteger result = first + second;

    first = second;
    second = result;

    yield return result;
  }
}

// Skip(1000) since it's defined that f(0) == 0 - unusual sequence starting;
// in mathematics sequences usually started from the 1st item
Console.Write(fibo().Skip(1000).FirstOrDefault().ToString());

The answer is

43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875




回答4:


Obviously, this calls for bc:

#!/usr/bin/bc --quiet

define fib(n) {
auto a,b,i;
if(n<2)return n;a=0;b=1;for(i=1;i<n;++i){c=a+b;a=b;b=c}return b;}

print fib(1000), "\n"
quit


来源:https://stackoverflow.com/questions/35745521/calculate-the-function-of-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!