Fibonacci Member
Level EASY
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
where F(0) = 0 and F(1) = 1
Input Format :
Output Format :
Constraints :
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
false
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def isPerfectSquare(x): | |
s = int(math.sqrt(x)) | |
return s * s == x | |
def isFibonacci(n): | |
return isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4) | |
num=int(input()) | |
if isFibonacci(num)==True: | |
print("true") | |
else:print("false") |
Comments
Post a Comment
Please give us your valuable feedback