Fibonacci-Python

Asked

Viewed 4,819 times

0

Construct a function, that is, an algorithm that receive the maximum number of the sequence of Fibonacci.

Returning all numbers from zero to zero Fibonacci number less than or equal to the number informed. Example:

Function - fibonacci(10)

Will return - 0,1,1,2,3,5,8

print (" Trabalho de Fibonacci ")

n =int( input(" Digite Quantos termos você deseja :"))
t1 = 0
t2 = 1

print("{} . {} .".format(t1,t2), end="")

cont = 6

while cont <= n:
    t3 = t1 + t2
    print ("  {}.".format(t3),end="")
    t1 = t2
    t2 = t3
    cont +=1

print( " Fim ")  

The code gives an error, I tried to change the cont for 3 but it didn’t work.

  • If you want to calculate the Fibonacci number less than or equal to the number reported, what this cont are you doing? Is he really necessary?

6 answers

2

Instead of using the variable cont to set the number of repetitions, use the variable itself t3, since she will be responsible for receiving the numbers from the Fibonacci sequence. Thus:

print (" Trabalho de Fibonacci ")

n =int( input(" Digite Quantos termos você deseja :"))
t1 = 0
t2 = 1
t3 = 0

while t3 <= n:
    print("{}.".format(t3), end="")
    t3 = t1 + t2
    t1 = t2
    t2 = t3

print( " Fim ")

I hope I helped, hug.

0

n = int(input('A sequência de Fibonacci de: '))
a, b = 0, 1
while a < n:
    print(a, end=',')
    a, b = b, a+b
print('FIM')

-1

I believe that way I can help you:

n =int( input(" Digite um teto para a sequência de Fibonacci :"))
t1 = 1
t2 = 0
t3 = 0
t4 = []

while t1 <= n:
    t4.append(t1)
    t3 = t1 + t2
    t2 = t1
    t1 = t3

print(t4)

-1

My code really works.

num = int(input())
def fibonacci(n):
    t1 = 0
    t2 = 1
    t3 = 0
    for _ in range(0, n):
        print(t1)
        t3 = t1 + t2
        t1 = t2
        t2 = t3
fibonacci(num)

-1

lista = ['1', '1']
for contador in range(2,int(input('Digite a quantidade de elementos da série fibonacci que deseja exibir:'))):
    lista.append(int(lista[len(lista) - 2]) + int(lista[len(lista) - 1]))
print(lista)

-2

x = input(int("Digite um inteiro positivo"))
a = 1; b = i = 0
print(f"Estes são os primeiros {x} termos da série de Fibonacci:")
print('\u26C4'*10)
while i < x:
    c = a + b
    a = b
    b = c
    i += 1
    print(a)
phi = a
  • You’re welcome to it, Hick. Two things: 1) read help (https://answall.com/help) whenever you have questions before posting anything, and if you notice at the top of the website editor there is a question mark (right side) that shows that clicking explains very well how to format 2) the ideal of the answers here is not only give a ready example that works, is explain the process. I haven’t tested your code, but looking over it looks like there’s something else that’s a print with a value that’s probably coloring the terminal and loop the variable c would already solve when closing (I did not look right)

  • Thank you. It has a Unicode character of a snowman that is not required for the code. I put instead of print('-'*10).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.