Hackerrank - Is Fibo Solution

Hackerrank - Is Fibo Solution

You are given an integer, . Write a program to determine if  is an element of the Fibonacci sequence.

The first few elements of the Fibonacci sequence are . A Fibonacci sequence is one where every element is a sum of the previous two elements in the sequence. The first two elements are  and .

Formally:

Input Format
The first line contains , number of test cases.
lines follow. Each line contains an integer .

Output Format
Display IsFibo if  is a Fibonacci number and IsNotFibo if it is not. The output for each test case should be displayed in a new line.

Constraints

Sample Input

3
5
7
8

Sample Output

IsFibo
IsNotFibo
IsFibo

Explanation
is a Fibonacci number given by
is not a Fibonacci number
is a Fibonacci number given by

Time Limit
Time limit for this challenge is given here.

Solution in Python

def solve(n):
    a = 0
    b = 1
    while a<n:
        a,b = b,a+b
    return "IsFibo" if a==n else "IsNotFibo"

for _ in range(int(input())):
    print(solve(int(input())))

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe