Fibonacci Number
You are given an integer, all you have to do is to find whether this number is a Fibonacci number or not.
Fn is said to be a Fibonacci sequence such that each number in Fn is the sum of its two preceding numbers, starting with 0 and 1.
Fn = F(n-1) + F(n-2)
fn is said to be a Fibonacci number if it is a part of the Fn/Fibonacci sequence.
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single integer ‘N’ which denotes the given number that has to check whether it is a Fibonacci number or not.
Output Format:
For each test case, print the “YES” if the number is a Fibonacci number. Otherwise, print “NO”.
Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of.
Constraints:
1 <= T <= 100
0 <= N <= 100000
Where ‘T’ is the number of test cases.
Where 'N' is the given number.
Time limit: 1 sec
CodingNinjas
author
2y
Brute Force
The basic idea is to keep generating Fibonacci numbers until we find a number equal to the given number or the generated number becomes greater than the given number. If the generated numbe...read more
CodingNinjas
author
2y
Mathematics
Java (SE 1.8)
C++ (g++ 5.4)
Python (3.5)
The basic idea of this approach is to check whether (5*n*n + 4) or (5*n*n - 4) is a perfect square or not. If they are, then ‘n’ is a Fibonacci number.
Reference: https://en.wikipedia.org/wiki/Fibonacci_number#Recognizing_Fibonacci_numbers
The steps are as follows:
- Store ( 5*n*n + 4 ) in ‘a’ and ( 5*n*n - 4) in ‘b’.
- Store square root of ‘a’ and ‘b’ in “aSq” and “bSq”, respectively.
- Return true if the square of “aSq” is equal to ‘a’ or the square of “bSq” is equal to ‘b’.
- Otherwise, return false.
O(1).
We are not using any extra space and so, the overall space complexity will be O(1).
Time Complexity: O(1)Explanation:O(log(N)), where ‘N’ is the given number.
We are using the sqrt() function that takes O(log(N)) time and so, the overall time complexity will be O(log(N)).
Java (SE 1.8)
/*
Time complexity: O(log(N))
Space complexity: O(1)
where 'N' is the given number.
*/
import java.util.ArrayList;
public class Solution
{
public static boolean CheckFiboNum(int n)
{
// As square of 'n' can be very large so we have to typecast it into long long.
long m = (long) n;
// Store the required values in 'a' and 'b'.
long a = 5 * m * m + 4;
long b = 5 * m * m - 4;
// Calculate their square roots.
long aSq = (long)Math.sqrt(a);
long bSq = (long)Math.sqrt(b);
// If any of them is a perfect sqaure then return true else, false.
return (aSq * aSq == a) || (bSq * bSq == b);
}
}
C++ (g++ 5.4)
/*
Time complexity: O(log(N))
Space complexity: O(1)
where 'N' is the given number.
*/
bool CheckFiboNum(int n)
{
// As square of 'n' can be very large so we have to typecast it into long long.
long long m = n;
// Store the required values in 'a' and 'b'.
long long a = 5 * m * m + 4;
long long b = 5 * m * m - 4;
// Calculate their square roots.
long long aSq = sqrtl(a);
long long bSq = sqrtl(b);
// If any of them is a perfect sqaure then return true else, false.
return (aSq * aSq == a) || (bSq * bSq == b);
}
Python (3.5)
'''
Time complexity: O(log(N))
Space complexity: O(1)
where ‘N’ is the given number.
'''
from math import sqrt
def CheckFiboNum(n):
# As square of 'n' can be very large so we have to typecast it into long long.
m = n
# Store the required values in 'a' and 'b'.
a = 5 * m * m + 4
b = 5 * m * m - 4
if(b < 0):
b = 0
# Calculate their square roots.
aSq = int(sqrt(a))
bSq = int(sqrt(b))
# If any of them is a perfect sqaure then return true else, false.
return (aSq * aSq == a) or (bSq * bSq == b)
Help your peers!
Add answer anonymously...
Top Twitter Software Developer interview questions & answers
Popular interview questions of Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app