Function to check whether a number is a Fibonacci number or not?
fibonacci, python, python-3.x
Solution
This is the most elegant solution i've encountered:
def is_fibonacci(n):
phi = 0.5 + 0.5 * math.sqrt(5.0)
a = phi * n
return n == 0 or abs(round(a) - a) < 1.0 / n
The code is not mine, was posted by @sven-marnach. The original post: check-input-that-belong-to-fibonacci-numbers-in-python
Problem
I've made a program which takes number of test cases as input and for each test case, it needs a number as input. Finally it checks whether the numbers you have entered are fibonacci numbers or not and prints accordingly. I've had no problems running it on my PC.But when i upload it to CodeChef.com(where i saw this quesion), it shows runtime error. Any help is appreciated and as i'm a noob my code might look lengthy ., any modifications are welcome.Thanks! Here's my code: ``` def isperfect(n): import math if n < 0: print("No Solution") return False else: test = int(math.sqrt(n)) return test*test == n test_cases = int(input()) count = 0 store = [] while count < test_cases: x = int(input()) store.append(x) count += 1 for each_item in store: assert isinstance(each_item, int) s1 = 5*each_item*each_item-4 s2 = 5*each_item*each_item+4 if(isperfect(s1) == True or isperfect(s2) == True): print("YES") else: print("NO") ```