+ 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
62 Respuestas
+ 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))
+ 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))
+ 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 :/ )
+ 9
just change code line = return 0 ==>return 1
+ 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)
+ 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
+ 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)
+ 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)
+ 3
https://code.sololearn.com/cJFXttGJ2OHz/?ref=app
check mine
+ 3
Do you have tasks working in python 3?
+ 3
Left that topic Tharyabha Manek
..
And have a happy coding👍🏻
😃
+ 3
Marko n is the input and we need to subtract 1 and 2
+ 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
+ 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
+ 2
You are just adding (a number below) and (two numbers below) the input. Fibonacci is adding the two previous numbers in the *list*
+ 2
Mr. Rahul int(input()) required for that
+ 2
Tharyabha Manek
I have used num = int(input())
🤔
+ 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()))
+ 1
Try if n<=0: return 0