+ 1

Fibonacci Sequence in Python

How would i write this formula?

11th Nov 2019, 1:42 AM
Sean Brown
Sean Brown - avatar
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))
22nd Nov 2020, 8:06 PM
Shahad K
Shahad K - avatar
+ 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.
24th Sep 2021, 9:17 PM
Serge
Serge - avatar
+ 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 ❤😁
11th Nov 2019, 4:56 AM
Baraa AB
Baraa AB - avatar
+ 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)
23rd Nov 2021, 3:46 PM
ANONyM
ANONyM - avatar
+ 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))
4th May 2021, 10:33 AM
Swaroop Kumar Badugu
Swaroop Kumar Badugu - avatar
+ 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)
13th Jul 2022, 1:46 PM
Elias DS
+ 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)
11th Jun 2021, 5:06 PM
Kala Anas
+ 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)}")
12th Feb 2022, 3:15 AM
achmad mustafa
achmad mustafa - avatar
+ 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
22nd May 2022, 12:05 AM
mnslla
mnslla - avatar
0
https://www.sololearn.com/learning/eom-project/1073/574 anyone else getting 404 error there?
17th Jan 2021, 1:35 PM
Patryk Wu
Patryk Wu - avatar
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)
6th Mar 2021, 7:51 PM
מאור הלפרין
מאור הלפרין - avatar
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)
23rd Sep 2021, 1:46 AM
Seedwagon
Seedwagon - avatar
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))
1st Mar 2022, 9:48 AM
Nahom Fekadu
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))
21st Jul 2022, 12:53 AM
Zocimo Orozco Valencia
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)
15th Dec 2022, 6:05 PM
Hidan
- 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}")
22nd Jul 2021, 3:49 AM
Shail Dave
Shail Dave - avatar
11th Nov 2019, 7:39 PM
Mehran sanea
Mehran sanea - avatar
- 2
k
21st Apr 2021, 7:19 AM
Muhd Hakimi Bin Abdullah
Muhd Hakimi Bin Abdullah - avatar
- 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)
14th Nov 2019, 10:54 PM
Sean Brown
Sean Brown - avatar