+ 1
Fibonacci Sequence in Python
How would i write this formula?
19 ответов
+ 54
num = int(input())
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
for number in range(num):
print(fibonacci(number))
+ 12
But how actually can we solve this? Exercise conditions pretty clear says u should get result from function call, not looping. So all the answers with loop after func init are wrong. Don't get proud.
+ 8
from math import sqrt
def F(n):
return ((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))
This is pretty easier 😅
And it is the seconde form of Fibonacci Sequence 😁
Good luck ❤😁
+ 4
num = int(input())
def fibonacci(n):
i = 0
myList = [0, 1]
while i < num - 2:
i += 1
myList.append(myList[-2] + myList[-1])
for x in range(len(myList)):
print(myList[x])
fibonacci(num)
+ 3
#this is the answer
num = int(input())
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
for number in range(num):
print(fibonacci(number))
+ 3
I used a nested function to do the looping inside the main function and it worked fine.
num = int(input())
def fibonacci(n):
def fib(n):
if n <= 1:
return n
else:
return fib(n-1)+fib(n-2)
for i in range(n):
print(fib(i))
fibonacci(num)
+ 1
num = int(input())
def fibonacci(n):
if n<=1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
for number in range(num):
print(fibonacci(number)
+ 1
my simpler code, only simpler
num = int(input("masukan angka: "))
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
elif n == 2:
return 1
elif n > 2:
return fib(n-1) + fib(n-2)
for n in range(num):
print(f"{fib(n)}")
+ 1
num = int(input())
while num < 0:
num = int(input())
first = 0
second = 1
for i in range(num):
print(first)
third = first + second
first = second
second = third
0
https://www.sololearn.com/learning/eom-project/1073/574
anyone else getting 404 error there?
0
There is no magic here... You'd have to add additional logic:
num = int(input())
temp=[]
temp2=[0,1,1]
def fibonacci(n):
#complete the recursive function
if n <= 1:
return 1
else:
f=(fibonacci(n-1) + fibonacci(n-2))
if n not in temp:
temp.append(n)
temp2.append(f)
return f
fibonacci(num)
for f in temp2[:-2]:
print(f)
0
#the best answer is
num = int(input())
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
for number in range(num):
print(fibonacci(number))
#But i did it like
def fibonacci(n, nOk = False):
if not nOk:
n=n-1
if n<=1:
ret = {0:0,1:1}
print(ret[n-1])
print(ret[n])
return ret
else:
prRet = fibonacci(n-1,True)
ret = prRet
ret[n] = prRet[n-1] + prRet[n-2]
print(ret[n])
return ret
fibonacci(num)
0
#fibonacci sequence
num = int(input())
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
for number in range(num):
print(fibonacci(number))
0
def fibonnaci(i):
#complete the recursive function
if i== 0:
return 0
elif i==1:
return 1
else:
return fibonnaci (i-1) + fibonnaci(i-2)
n=int(input())
for n in range(0,n):
print(fibonnaci(n))
0
num = int(input())
def fibonacci(n):
first = [0, 1]
for i in range(n-2):
first.append(first[-1] + first [-2])
for i in first:
print(i)
fibonacci(num)
- 1
n = int(input())
def fibonacci(cnt,n,l,a1,a2,ans):
# cnt=cnt+1
if cnt==n:
return l
else:
l.append(a2)
ans = a1 + a2
a1 = a2
a2 = ans
return fibonacci((cnt+1),n,l,a1,a2,ans)
l=[]
a1 = 0
a2 = 1
ans = 0
l.append(a1)
l2=fibonacci(1,n,l,a1,a2,ans)
for i in l:
print(f"{i}")
- 2
k
- 9
I got one.
num1=0
num2=1
for x in ans
ans1=num1+num2
num1= ans1
ans2 =num1+num2
num2 =ans2
print(num1)
print(num2)