简体   繁体   English

斐波那契数列从a点到b点?

[英]fibonacci sequence going from point a to point b?

m = 2
n =20
a,b = m,0
fib = [m]
while a <= n:
   fib.append(a)
   a,b = a+b, a

So given two variables from m to n (and m < n) , I need to create a list containing all the numbers of the Fibonacci sequence between m and n inclusive (but cannot exceed) ex: if m = 2 and n = 20 then fib should be [2,3,5,8,13] . 因此,给定两个从mn (and m < n)变量,我需要创建一个列表,其中包含mn之间(包括但不能超过)的所有斐波那契数列的数字(但不能超过)ex:如果m = 2n = 20fib应该是[2,3,5,8,13]

I do not know how to start the fibonnaci sequence midway, so the best I can think of is to filter the results afterwards. 我不知道如何从中途开始斐波那契序列,所以我能想到的最好的办法是随后过滤结果。

def f(low, high):
    fib = [0]
    a, b = 1, 0
    while a <= n:
       fib.append(a)
       a,b = a+b, a
    return filter(lambda x: x >= low and x =< high, fib)

The fibonacci code is trivial, the new thing you might be seeing here is filter , which takes a function f and an iterable x , and returns a new iterable with all of the elements from x such that f(x) is true. 斐波那契代码是微不足道的,您可能在这里看到的新东西是filter ,它接受一个函数f和一个可迭代的x ,并返回一个带有来自x所有元素的新的可迭代对象,使得f(x)为true。

def fib(m,n):
    a,b = 1,1
    while a < m:
        a,b = b, a+b

    answer = [a]
    while b < n:
        a,b = b, a+b
        answer.append(a)
    return answer

In [2040]: fib(2,20)
Out[2040]: [2, 3, 5, 8, 13]
m  = int(raw_input("Enter the start number : "))
n = int(raw_input("Enter the end number : "))
def fib(i):
if i == 0: return 0
elif i == 1: return 1
else: return f(i-1)+f(i-2)
print map(fib, range(m, n))

I hope this is what you need. 我希望这是您所需要的。

I thanks it's simple and clear to calculate the Fibonacci number recursively or by put all the number in a list. 我感谢以递归方式或通过将所有数字都放在列表中来计算斐波纳契数很简单明了。 But if the number is too large, it not a good idea. 但是,如果数量太大,那不是一个好主意。 Here is code ,BTW 这是代码,顺便说一句

def main():
    print fibo(100,600)
def fibo(m,n):
    f0=2
    f1=3
    while f1<m:
        tmp=f1
        f1=f0+f1
        f0=tmp
    res=[f0]
    while f1<n:
        res.append(f1)
        f1=res[-2]+res[-1]
    return res[1:];

if __name__ == '__main__':
    main()

I googled and find the n-th term formula of fibonacci here 我用Google搜索,并找到斐波那契的第n个项公式在这里

so the codes could be: 因此代码可能是:

def fibn(n):
     Phi = (1+math.sqrt(5))/2
     phi = (1-math.sqrt(5))/2
     return round((math.pow(Phi, n) - math.pow(phi, n))/math.sqrt(5))

>>> fibn(0)
0.0
>>> fibn(1)
1.0
>>> fibn(2)
1.0
>>> fibn(3)
2.0
>>> fibn(4)
3.0
>>> fibn(5)
5.0
>>> fibn(6)
8.0
>>> fibn(7)
13.0
>>> fibn(8)
21.0
>>> fibn(9)
34.0
>>> fibn(10)
55.0

You could do something like: 您可以执行以下操作:

def fibs(low,high):
    a, b = 0, 1
    while 1:
        a, b = b, a+b
        if low <= a:
            if a <= high:
                yield a
            else:
                break

you can use it like 你可以像这样使用它

>>> for num in fibs(2,15):
...     print num
... 
2
3
5
8
13

But without resorting to the formula for the nth Fibonacci number and relying on proper rounding there isn't a way of getting the nth number without computing the first n-1 numbers. 但不诉诸于式为nth Fibonacci数和依靠适当的四舍五入没有得到的一种方式nth编号不计算所述第一n-1数字。

So, if you don't want to use the formula it would probably be best to just keep a list of the Fibonacci numbers around and use that, if it turns out you need numbers between low and high where high > fib_nums[-1] then you can always use fib_nums[-1] and fib_nums[-2] as b and a to compute the values you're missing. 因此,如果您不想使用该公式,则最好只保留一个斐波纳契数的列表并使用它,如果事实证明您需要在lowhigh数之间的数字,其中high > fib_nums[-1]那么您始终可以将fib_nums[-1]fib_nums[-2]用作ba来计算所缺少的值。

There are a few subproblems to consider for getting a log order solution, assuming (nm) is relatively small. 假设(nm)相对较小,要获取对数顺序解决方案,需要考虑一些子问题。 If (nm) can be relatively large its best to precompute all reults and simply do a binary search. 如果(nm)可能相对较大,则最好预先计算所有结果并简单地执行二进制搜索。

  1. Can we find i th fibonacci number in log time? 我们可以在登录时间内找到斐波那契数吗?
  2. Can we find the number j such that fib(j) >= m ? 我们可以找到j使得fib(j)> = m吗?

For first problem we can find i th fibonacci using ( http://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form ). 对于第一个问题,我们可以使用( http://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form )找到斐波那契。

And the second problem can be solved using a binary search and uses first method to find the fibonacci number >= m. 第二个问题可以使用二进制搜索解决,并使用第一种方法找到斐波那契数> = m。 Once we know j we can find j+1 th fibonacci number in log time, and simply generate all other numbers <=n using these. 一旦知道j,我们就可以在对数时间中找到第j + 1个斐波那契数,并使用它们简单地生成所有其他数<= n。

Using Generator : 使用生成器:

import os,sys

def fib(num):
    a=0
    b=1
    while 1:
        a,b =b, b+a
        yield a

low=2
high=200
for i in fib(range(1)):
    if i <= high and i >= low :
        print i
    elif i > high:
        break

O/P 2 3 5 8 13 21 34 55 89 144 O / P 2 3 5 8 13 21 34 55 89144

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM