Given a number n, check if n is a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ..
Examples :
Input: n = 34
Output: true
Explanation: 34 is one of the numbers of the Fibonacci series.
Input: n = 41
Output: false
Explanation: 41 is not in the numbers of the Fibonacci series.
Table of Content
Generate Fibonacci Numbers Iteratively - O(log n) Time O(1) Space
The idea is to generate Fibonacci numbers one by one starting from
0and1until the generated number becomes greater than or equal ton.
- If the generated Fibonacci number becomes equal to
n, thennis a Fibonacci number.- Otherwise,
nis not a Fibonacci number.
#include <bits/stdc++.h>
using namespace std;
bool isFibonacci(int n)
{
// Base cases
if (n == 0 || n == 1)
{
return true;
}
int a = 0;
int b = 1;
int c = a + b;
// Generate Fibonacci numbers
while (c < n)
{
a = b;
b = c;
c = a + b;
}
return c == n;
}
// Driver Code
int main()
{
int n = 34;
if (isFibonacci(n))
{
cout << "true";
}
else
{
cout << "false";
}
return 0;
}
public class GfG {
public static boolean isFibonacci(int n)
{
// Base cases
if (n == 0 || n == 1)
{
return true;
}
int a = 0;
int b = 1;
int c = a + b;
// Generate Fibonacci numbers
while (c < n)
{
a = b;
b = c;
c = a + b;
}
return c == n;
}
// Driver Code
public static void main(String[] args)
{
int n = 34;
if (isFibonacci(n))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
def isFibonacci(n):
# Base cases
if n == 0 or n == 1:
return True
a = 0
b = 1
c = a + b
# Generate Fibonacci numbers
while c < n:
a = b
b = c
c = a + b
return c == n
# Driver Code
if __name__ == '__main__':
n = 34
if isFibonacci(n):
print('true')
else:
print('false')
using System;
public class GfG
{
public static bool IsFibonacci(int n)
{
// Base cases
if (n == 0 || n == 1)
{
return true;
}
int a = 0;
int b = 1;
int c = a + b;
// Generate Fibonacci numbers
while (c < n)
{
a = b;
b = c;
c = a + b;
}
return c == n;
}
// Driver Code
public static void Main()
{
int n = 34;
if (IsFibonacci(n))
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
}
}
function isFibonacci(n) {
// Base cases
if (n == 0 || n == 1) {
return true;
}
let a = 0;
let b = 1;
let c = a + b;
// Generate Fibonacci numbers
while (c < n) {
a = b;
b = c;
c = a + b;
}
return c == n;
}
// Driver Code
let n = 34;
if (isFibonacci(n)) {
console.log('true');
} else {
console.log('false');
}
Output
true
Time Complexity: O(log n)
Space Complexity: O(1)
Using Perfect Square Property - O(1) Time O(1) Space
A number
nis a Fibonacci number if either of the following is a perfect square: 5n2 + 4 or 5n2 − 4. So, we calculate both values and check if any one of them is a perfect square. If any one of them is a perfect square, then n is a Fibonacci number.
This is a mathematical property of Fibonacci numbers: 5n2 + 4 or 5n2 − 4.
#include <bits/stdc++.h>
using namespace std;
// Check if x is a perfect square
bool isPerfectSquare(int x)
{
int s = sqrt(x);
return s * s == x;
}
// Check if n is a Fibonacci number
bool isFibonacci(int n)
{
int val1 = 5 * n * n + 4;
int val2 = 5 * n * n - 4;
return isPerfectSquare(val1) || isPerfectSquare(val2);
}
int main()
{
int n = 34;
if (isFibonacci(n))
cout << "true";
else
cout << "false";
return 0;
}
import java.lang.Math;
// Check if x is a perfect square
public class GfG {
public static boolean isPerfectSquare(int x)
{
int s = (int) Math.sqrt(x);
return s * s == x;
}
// Check if n is a Fibonacci number
public static boolean isFibonacci(int n)
{
int val1 = 5 * n * n + 4;
int val2 = 5 * n * n - 4;
return isPerfectSquare(val1) || isPerfectSquare(val2);
}
public static void main(String[] args)
{
int n = 34;
if (isFibonacci(n))
System.out.println("true");
else
System.out.println("false");
}
}
import math
# Check if x is a perfect square
def isPerfectSquare(x):
s = int(math.sqrt(x))
return s * s == x
# Check if n is a Fibonacci number
def isFibonacci(n):
val1 = 5 * n * n + 4
val2 = 5 * n * n - 4
return isPerfectSquare(val1) or isPerfectSquare(val2)
n = 34
if isFibonacci(n):
print("true")
else:
print("false")
using System;
// Check if x is a perfect square
public class GfG
{
public static bool isPerfectSquare(int x)
{
int s = (int)Math.Sqrt(x);
return s * s == x;
}
// Check if n is a Fibonacci number
public static bool isFibonacci(int n)
{
int val1 = 5 * n * n + 4;
int val2 = 5 * n * n - 4;
return isPerfectSquare(val1) || isPerfectSquare(val2);
}
public static void Main()
{
int n = 34;
if (isFibonacci(n))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
// Check if x is a perfect square
function isPerfectSquare(x) {
let s = Math.sqrt(x);
return s * s === x;
}
// Check if n is a Fibonacci number
function isFibonacci(n) {
let val1 = 5 * n * n + 4;
let val2 = 5 * n * n - 4;
return isPerfectSquare(val1) || isPerfectSquare(val2);
}
let n = 34;
if (isFibonacci(n)) {
console.log('true');
} else {
console.log('false');
}
Output
true
Time Complexity: O(1)
Space Complexity: O(1)