Python Program to Check Fibonacci Member

Given a number N, figure out if it is a member of fibonacci series or not. Return true if the number is a member of the Fibonacci series else false.

Input Format :

Integer N

Output Format :

true or false

Constraints :

0 <= n <= 10^4

Sample Input 1 :

true

Sample Output 1 :

Integer N

Sample Output 2 :

false

Solution :

  1. def checkMember(n):
  2.     temp = 1
  3.     a = 1
  4.     b = 0
  5.     c = 0
  6.     if n == 0:
  7.         return True
  8.     elif n == 1:
  9.         return True
  10.     else:
  11.         while temp <= n:
  12.             c = a + b
  13.             b = a
  14.             a = c
  15.             if n == c:
  16.                 return True
  17.                 break
  18.             
  19.            
  20.             temp = temp + 1
  21.         return False
  22.             
  23.             
  24.     

  25. n=int(input())
  26. if(checkMember(n)):
  27.     print("true")
  28. else:
  29.     print("false")
Previous Post Next Post