如何在Python中将斐波那契序列打印到第n个数字? [英] How do I print a fibonacci sequence to the nth number in Python?

查看:197
本文介绍了如何在Python中将斐波那契序列打印到第n个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份家庭作业,很麻烦.我正在尝试编写一个程序,输出第n个数字的斐波那契数列.这是我到目前为止的内容:

I have a homework assignment that I'm stumped on. I'm trying to write a program that outputs the fibonacci sequence up the nth number. Here's what I have so far:

def fib():
   n = int(input("Please Enter a number: "))

   if n == 1:
      return(1)
   elif n == 0:   
      return(0)            
   else:                      
      return (n-1) + (n-2)


mylist = range[0:n]
print(mylist)

我想我可以使用单独的函数,但是我不知道如何传递计算斐波那契数列的参数.然后,下一步将是打印出直到该数字为止的数字序列.

I'm thinking I could use separate functions but I can't figure out how to pass the argument that calculates the fibonacci sequence. Then the next step would be to to print out the sequence of numbers up to that number.

推荐答案

非递归解决方案

def fib(n):
    cur = 1
    old = 1
    i = 1
    while (i < n):
        cur, old, i = cur+old, cur, i+1
    return cur

for i in range(10):
    print(fib(i))

发电机解决方案:

def fib(n):
    old = 0
    cur = 1
    i = 1
    yield cur
    while (i < n):
        cur, old, i = cur+old, cur, i+1
        yield cur

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

请注意,生成器解决方案的性能优于非递归解决方案(如果对递归解决方案不应用备注,则非递归性能优于递归)

Note that generator solution outperforms the non-recursive (and non-recursive outperforms recursive, if memoization is not applied to recursive solution)

再一次,具有记忆递归:

One more time, recursive with memoization:

def memoize(func):
    memo = dict()
    def decorated(n):
        if n not in memo:
            memo[n] = func(n)
        return memo[n]

    return decorated

@memoize
def fib(n):
    #added for demonstration purposes only - not really needed
    global call_count
    call_count = call_count + 1
    #end demonstration purposes

    if n<=1:
        return 1
    else:
        return fib(n-1) + fib(n-2)

call_count = 0 #added for demonstration purposes only - not really needed
for i in range(100):
    print(fib(i))
print(call_count) #prints 100

这次每个斐波纳契数精确地计算一次,然后存储.因此,该解决方案将胜过所有其他解决方案.但是,装饰器实现只是快速而肮脏的,不要让它进入生产环境. (有关python装饰器的详细信息,请参见SO上的这个令人惊讶的问题:)

This time each fibbonacci number calculated exactly once, and than stored. So, this solution would outperform all the rest. However, the decorator implementation is just quick and dirty, don't let it into production. (see this amazing question on SO for details about python decorators :)

因此,在定义了fib之后,该程序将类似于(对不起,只是循环很无聊,下面是一些更酷的python工具:

So, having fib defined, the program would be something like (sorry, just looping is boring, here's some more cool python stuff: list comprehensions)

fib_n = int(input("Fib number?"))
fibs = [fib(i) for i in range(fib_n)]
print " ".join(fibs) 

这会在一行中打印所有数字,并用空格分隔.如果您希望每条线都单独显示-将" "替换为"\n"

this prints all numbers in ONE line, separated by spaces. If you want each on it's own line - replace " " with "\n"

这篇关于如何在Python中将斐波那契序列打印到第n个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆