+ 3
Fibonacci on python
num = int(input()) def fibonacci(n): #complete the recursive function if n <= 1: return 0 else: x, y = 0, 1 for i in range(n): x, y = y, x+y print(x) #print("0") fibonacci(num-1) “”” I ran the code multiple times, and the zero won’t show up can someone help me figure out what I’m missing here. “””
15 Respostas
- 2
Just put the print(x) before the x, y =...
By the time you print x is already 1
+ 5
I wrote this code:
num = int(input())
def fibonacci(n):
# code goes here
if n == 0:
return n
elif n == 1:
print(n - 1)
return (0, 1)
else:
prev = fibonacci(n - 1)
a, b = prev
a, b = b, a + b
print(a)
return (a, b)
fibonacci(num)
+ 5
num = int(input())
def fibonacci(n):
#complete the recursive function
if n <= 1:
return 0
else:
x, y = 1, 0
for i in range(n):
x, y = y, x+y
print(x)
fibonacci(num)
+ 3
I wanted my solution to be recursive as asked, and not needing an extra loop for the printing task.
Here is my best solution yet.
num = int(input())
def fibonacci(n):
"""
RECURSIVELY calculates and prints the n
first numbers of the fibonacci sequence
n has to be an integer greater than zero
fibonacci(n) returns a tupple with the
n'th and the (n+1)'th fibonacci number
"""
if n > 1:
prev = fibonacci(n - 1)
# create a new tupple
# with the last given fibonacci number
# and the calculated next one
new = (prev[1], prev[0]+prev[1])
print(new[0])
return(new)
else:
# print the first fibonacci number and
# return the two first fibonacci numbers
print(0)
return (0, 1)
fibonacci(num)
# OLD VERSION
# needs lots of reruns of f(n) to calculate
# and print each value separately
#
#def fibonacci(n):
# def f(n):
# if n > 1:
# return f(n-1) + f(n-2)
# elif n == 1:
# return 1
# else:
# return 0
#
# for i in range(n):
# print(f(i))
+ 2
Yeriel Vanderhost
Print x before for loop
num = int(input())
def fibonacci(n):
#complete the recursive function
if n <= 1:
return 0
else:
x, y = 0, 1
print (x)
for i in range(n):
x, y = y, x + y
print(x)
fibonacci(num - 1)
+ 2
x,y = 0,1 change it to x,y =1,0 and you are good to go
+ 1
Carlito, where is recursion in your code?
+ 1
num = int(input())
def fibonacci(n):
if n <= 1:
return 0
else:
x, y = 0, 1
for i in range(n):
print(x)
x, y = y, x+y
fibonacci(num)
I used the original commentor code but I change it up so that you could print 0 as initially it was calculating before printing that is why, it doesn't print 0. Thank you Yeriel Vanderhost, otherwise I would be stuck.
0
Alguien me pasa el código que si le haga ejecutado por favor ya vi muchos videos y me sale error
Someone passes me the code that if I make it executed please I already saw many videos and I get error
0
num = int(input())
def fibonacci(n):
# code goes here
if n == 0:
return n
elif n == 1:
print(n - 1)
return (0, 1)
else:
prev = fibonacci(n - 1)
a, b = prev
a, b = b, a + b
print(a)
return (a, b)
fibonacci(num)
0
num = int(input())
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
for i in range(num):
print(fibonacci(i))
0
num = int(input())
def fibonacci(n):
if n<=1:
return 0
else:
x,y = 0,1
print(x)
for i in range(n-1):
x,y = y, x+y
print(x)
fibonacci(num)
0
num = int(input())
fir=0
sec=1
def fibonacci(n,fir,sec):
if n<=1:
return n
else :
sum1=fir+sec
print(sum1)
fir=sec
sec=sum1
return fibonacci(n-1,fir,sec)
#complete the recursive function
print(fir)
print(sec)
fibonacci(num-1,fir,sec)
0
Hope it will help you:
num = int(input())
def fibonacci(n):
#complete the recursive function
a = 0
b = 1
if n==0:
return print (a)
if n==1:
print (a)
print (b)
return
else:
print(a)
print(b)
for i in range(2,n):
c = a+b
a = b
b = c
print (c)
fibonacci(num)
Good luck!
- 1
num = int(input())
def loop(n):
for i in range(0, n, 1):
def fib(i):
if i == 0:
return 0
elif i == 1:
return 1
else:
return fib(i-2) + fib(i-1)
print(fib(i))
loop(num)