Please help to rectify this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please help to rectify this code

Given a number N, figure out if it is a member of fibonacci series or not. Return true if the number is member of fibonacci series else false. Fibonacci Series is defined by the recurrence F(n) = F(n-1) + F(n-2) where F(0) = 0 and F(1) = 1 Code- def checkMember(n): #Implement Your Code Here pass F(n) = F(n-1) + F(n-2) n=int(input()) if(checkMember(n)): print("true") else: print("false") I also don't understand the meaning of pass here it must be return I think. One another code I have tried but it's working for three test cases only def recur_fibo(n): if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) n = int(input()) # check if the number of terms is valid if n < 0: print("false") else: print("true")

21st Jul 2021, 6:27 PM
Student
Student - avatar
2 Answers
21st Jul 2021, 6:47 PM
JaScript
JaScript - avatar
+ 1
pass isn't a returning keyword, return is the only one that does. pass is basically a no-op statement, as in it does nothing. All it is is a placeholder statement so you can leave empty bodies for if's, functions, classes, and loops. You remove these statements once you have finished implementing the required code. Practical coding typically leaves a comment that says "#FIXME" or "#IMPLEMENT ME!" so that the developer can come back later and see he/she needs to work on it some more. For the code itself, it would be more helpful if you showed what the test cases that worked were. However, let's look at it without the test info. The Fibonacci sequence, if you didn't know, is a sequence of numbers defined by the sum of the previous two numbers, starting from 0, and the second number is 1. The sequence is such for the first 10 elements: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.... that means, in order the calculate the next number in the sequence, we need the previous number, and the current number. What you're doing right now, though, is you're taking n, and subtracting from it, which isn't doing anything. n is merely a counter, not a variable that needs to be manipulated in any way, other than decrementing until 0 is reached. You should have two parameters in your recursive function that holds the previous and current numbers in the sequence, which is passed from the previous function call. The code would look something like this: def fib(n=0, current=1, prev=0): if n <= 1: return prev else: return fib(n - 1, current + prev, current) DISCLAIMER: I have not tested this, and will most likely not work, because doing logic in my head isn't reliable.
21st Jul 2021, 7:14 PM
BootInk
BootInk - avatar