+ 12

Fibonacci

def fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else: return fibonacci(n-1)+fibonacci(n-2) n=int(input()) print(fibonacci(n)) The Fibonacci sequence is one of the most famous formulas in mathematics. Each number in the sequence is the sum of the two numbers that precede it. For example, here is the Fibonacci sequence for 10 numbers, starting from 0: 0,1,1,2,3,5,8,13,21,34. Write a program to take N (variable num in code template) positive numbers as input, and recursively calculate and output the first N numbers of the Fibonacci sequence (starting from 0). Sample Input 6 Sample Output 0 1 1 2 3 5 What's wrong in my code

13th Nov 2020, 8:13 AM
Harshita Kumari
Harshita Kumari - avatar
62 odpowiedzi
+ 109
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))
2nd Jan 2021, 11:13 PM
Jørgen Eliassen
Jørgen Eliassen - avatar
+ 27
I solved it myself n=int(input()) def fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else: return fibonacci(n-1)+fibonacci(n-2) for n in range(n): print(fibonacci(n))
13th Nov 2020, 11:56 AM
Harshita Kumari
Harshita Kumari - avatar
+ 11
I did this : num = int(input()) def fibonacci(n): if n <= 1: return (0) elif n == 2: return (1) else: return (fibonacci(n - 1) + fibonacci(n - 2)) for i in range(num): print(fibonacci(i + 1)) It took me a while (way too long) to realize that the assignement didn’t stop us to modify the existing code (which is why I don’t understand the purpose of this code, pls just let us code from scratch, same thing for the fizzbuzz, for me it’s just more confusing :/ )
26th Nov 2020, 12:58 AM
Curtis
Curtis - avatar
+ 9
just change code line = return 0 ==>return 1
15th Nov 2020, 1:55 AM
Samet
Samet - avatar
+ 8
Here is my owncode: num = int(input()) def fibonacci(n): a=0 b=1 if n==1: return a elif n==2: return b else: print(a) print(b) for i in range(0,n-2): c=a+b print(c) a=b b=c fibonacci(num)
23rd Nov 2020, 7:08 AM
GNG
GNG - avatar
+ 5
Alternative one/Easiest one🤗 num = int(input()) n1,n2 = 0,1 count = 0 if num == 1: print(n1) while count < num: print(n1) nth = n1 + n2 n1 = n2 n2 = nth count += 1
13th Nov 2020, 2:03 PM
Mr. Rahul
Mr. Rahul - avatar
+ 5
num = int(input()) def fibonacci(n): a=0 b=1 count=0 while count<=n-1: a,b=b+a,a count+=1 print(b) fibonacci(num)
21st Mar 2021, 12:29 AM
Monir Farag
Monir Farag - avatar
+ 4
Not n<=1, that should be n==1; and it will give fibonacci sequence starting from 1 To get Fibonacci sequence starting from 0.. you should write your code as: if n<0: return 0 if n==1: return 0 //cause your 1st place is 0 if n==2 or n== 3: return 1 //cause 2nd and 3rd place are both 1 else return fib(n-1)+fib(n-2)
13th Nov 2020, 8:55 AM
Blue!!
Blue!! - avatar
3rd Oct 2021, 1:40 PM
Prabhas Koya
+ 3
https://code.sololearn.com/cJFXttGJ2OHz/?ref=app check mine
13th Nov 2020, 1:18 PM
Somil Khandelwal
+ 3
Do you have tasks working in python 3?
14th Nov 2020, 1:42 PM
kipi03
kipi03 - avatar
+ 3
Left that topic Tharyabha Manek .. And have a happy coding👍🏻 😃
22nd Dec 2020, 12:47 PM
Mr. Rahul
Mr. Rahul - avatar
+ 3
Marko n is the input and we need to subtract 1 and 2
12th Jan 2021, 5:28 AM
Harshita Kumari
Harshita Kumari - avatar
+ 3
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)) its a very easy way to do this project but for those guys dont know what is doing this code specially the Fibunacci in Fibunacci function, so there is the deal when i put variable for fibunacci, fibunacci do the math and that is: if input be 5: i = 0, 1 ,2 ,3 ,4 so we place this variable in the function,like: 0, 1 will print cuas of the first statement but about next statement(after else) it just doing as place and bring back the number of that place, like: 2 = fibunacci(2-1) => fibunacci(1) and fibunacci(1) print 1 place of everynumber it until now printed! 0 , 1 take it as a list, [0, 1], the 1 place of it is 1 now you know fibunacci (1)print back which number
13th Feb 2021, 10:50 AM
Ali Kelidari
Ali Kelidari - avatar
+ 3
num = int(input()) m=0 n=1 print (m) for i in range(num-1): print (m+n) m,n=m+n,m This is my solution. But not recursive
4th Apr 2021, 10:05 AM
Shahzod Sayfiddinov
Shahzod Sayfiddinov - avatar
+ 2
You are just adding (a number below) and (two numbers below) the input. Fibonacci is adding the two previous numbers in the *list*
13th Nov 2020, 8:29 AM
Olivia
Olivia - avatar
+ 2
Mr. Rahul int(input()) required for that
21st Dec 2020, 3:41 PM
Yondaime
Yondaime - avatar
+ 2
Tharyabha Manek I have used num = int(input()) 🤔
22nd Dec 2020, 3:36 AM
Mr. Rahul
Mr. Rahul - avatar
+ 2
I did this : def fibonacci(i, j, n): print (i) i, j = j, i + j n -= 1 if 0 < n: fibonacci (i, j, n) fibonacci(0, 1, int(input()))
28th Jul 2021, 5:25 PM
Manogya Singh
Manogya Singh - avatar
+ 1
Try if n<=0: return 0
13th Nov 2020, 8:30 AM
Blue!!
Blue!! - avatar