Skip to content

Instantly share code, notes, and snippets.

@voyeg3r
Created March 11, 2010 11:59
Show Gist options
  • Select an option

  • Save voyeg3r/329060 to your computer and use it in GitHub Desktop.

Select an option

Save voyeg3r/329060 to your computer and use it in GitHub Desktop.
python - sequencia de fibonacci
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment