开始Python斐波纳契生成器

时间:2013-11-07 14:15:55

标签: python fibonacci

我正在尝试制作一个停止在给定数量的斐波纳契数字生成器,但它通常会超过该数量。我做错了什么?

#Fibonacci number generator
a=0
b=1
print("Fibonacci number generator.")
stopNumber=input("How high do you want to go? If you want to go forever, put n.")
print(1)
while stopNumber=="n":
        a=a+b
        b=b+a
        print(a)
        print(b)
else:
    while int(stopNumber) > a or int(stopNumber) > b:
        a=a+b
        b=b+a
        print(a)
        print(b)

7 个答案:

答案 0 :(得分:1)

您获得更高价值的原因是因为您在单个循环中拥有a = a+bb = b+a。因此,当您检查while int(stopNumber) > a or int(stopNumber) > b:中的值时,您会获得True并输入循环,但a = a+bb = b+a可以生成a和{{1}的值大于b并且由于您在不检查的情况下打印它,因此您获得了更高的值。你应该在循环中只增加一次,如果你在while循环之后编写print语句,你将得不到正确的值

stopNumber

注意:如果输入为prev = 0 curr = 1 print("Fibonacci number generator.") stopNumber = input("How high do you want to go? If you want to go forever, put n.") if stopNumber == 'n': print(curr) curr = prev + curr prev = curr else: while curr<stopNumber: print(curr) curr = prev + curr prev = curr ,则代码将永久运行。

答案 1 :(得分:1)

同样的,工作和使用一些更智能的技术:

# returns generator
def fib(stop):
    prev, current = 0, 1
    while current < stop:  # a little hack here - python is ok comparing ints to floats
        yield current
        # multiple assginment - operands on the left are "frozen" just before theis instruction
        prev, current = current, prev + current 

# note inf - float('inf') results in "positive infinity" which is an appropriate math concept for "forever"
stop = float(input("How high do you want to go? If you want to go forever, put inf."))

for f in fib(stop):
    print (f)

注意:请不要尝试list(fib(float('inf'))):)

答案 2 :(得分:0)

你检查一下stopNumber&gt; a或b,然后增加a和b,打印它们。如果你只想打印它们是&lt; = stopNumber而不是像这样:

#Fibonacci number generator
a=0
b=1
print("Fibonacci number generator.")
stopNumber=input("How high do you want to go? If you want to go forever, put n.")
print(1)
while stopNumber=="n":
        a=a+b
        b=b+a
        print(a)
        print(b)
else:
    while True:
        a = a+b
        b = b+a
        if int(stopNumber) >= a:
           print(a)
        if int(stopNumber) >= b:
           print(b)
        else:
          break

答案 3 :(得分:0)

使用您的代码:

#Fibonacci number generator
a=0
b=1
print("Fibonacci number generator.")
stopNumber=input("How high do you want to go? If you want to go forever, put n.")
print(1)
while stopNumber=="n" or int(stopNumber) > a+b:
    a, b = b, a+b
    print(b)

答案 4 :(得分:0)

第二个&#34;而&#34;循环始终保持运行&#34; a&#34;或&#34; b&#34;低于&#34; stopNumber&#34;。因此,循环继续运行,直到BOTH&#34; a&#34;和&#34; b&#34;大于&#34; stopNumber&#34;。因此,当&#34; b&#34;大于&#34; stopLimit&#34;但是&#34; a&#34;仍低于&#34; stopLimit&#34;循环继续运行。因此,第一个应用的修复方法是更改​​&#34;或&#34;条件由&#34;和&#34;之一。

您只是在总和之前检查条件是否适用。然后,在完成总和的那一刻,他们的结果可能大于&#34; stopLimit&#34 ;;这就是你打印的东西。要解决此问题,您可以添加&#34; if&#34;用于验证总和结果仍然低于&#34; stopNumber&#34;。

的声明

以下是这些修补程序的代码:

#Fibonacci number generator
a=0
b=1
print("Fibonacci number generator.")
stopNumber=input("How high do you want to go? If you want to go forever, put n.")
print(1)
while stopNumber=="n":
        a=a+b
        b=b+a
        print(a)
        print(b)
else:
    while int(stopNumber) > a and int(stopNumber) > b:
        a=a+b
        b=b+a
        if int(stopNumber) > a:
            print(a)
        if int(stopNumber) > b:
            print(b)

答案 5 :(得分:0)

 #This is a simple yet efficient program using recursion:

def fibonacci_rec(a, b):
    if a >= 0 and b > 0 or a > 0 and b >= 0:
        s = [a, b]
        while a+b <= 1000: #can set any upper boundary  
            f = a+b
            a = b
            b = f
            s.append(f)
            fibonacci_rec(a, b)
        return s
    else:
        return 'Invalid'

print(fibonacci_rec(1, 1))  # You can set any value of a and b, as you like and no need to iterate in here, just call function once and it does the iteration for you! 

答案 6 :(得分:-1)

def fib(n):
    if n == 0:
        return 0
    if n == 1:
        return 1
    return fib(n-1) + fib(n-2)

print("Fibonacci number generator.")
stopNumber=input("How high do you want to go? If you want to go forever, put n.")
if stopNumber == 'n':
    i=1
    while True:
        print 'Fibonacci #{0}: {1}'.format(i, fib(i))
        i=i+1
else:
    for i in range(1,int(n)):
        print 'Fibonacci #{0}: {1}'.format(i, fib(i))