Python - sequencia de fibonacci - melhore este código, comente

#!/usr/bin/env python
# # -*- coding: UTF-8 -*-
# Criado em:Qui 11/Mar/2010 hs 08:19
# Last Change: Qui 11 Mar 2010 08:52:48 BRT
# vim:ft=python:nolist:nu:
# Instituicao: none
# Proposito do script: calcular sequencia de fibonacci
# Autor: Sérgio Luiz Araújo Silva
# site: http://vivaotux.blogspot.com
"""
importe: from fibonacci import fib
g = fib
for i in range(max):
print g.next(),
"""
from __future__ import generators
import os
# limpando a tela
if os.name == 'posix':
os.system('clear')
else:
os.system('cls')
# needs Python 2.2 or above!
def fib():
"unbounded generator, creates Fibonacci sequence"
x,y=0,1
while 1:
x, y = y, x + y
yield x
if __name__ == "__main__":
g = fib()
max = int(raw_input("Digite o numero a calcular: "))
for i in range(max):
print g.next(),
# O Magnun Leno http://www.blogger.com/profile/04716536861057964759
# enviou uma versão com recursividade:
#def fib(max, x=0, y=1):
# next = x + y
# if next >= max:
# print x,y
# return
# print x,
# fib(max, y, next)
#
## Para executar:
#>>> fib(10)
#0 1 1 2 3 5 8
view raw fibonacci.py hosted with ❤ by GitHub

linux-cookbook

Grupos do Google
Participe do grupo linux-cookbook
E-mail:
Visitar este grupo