Check Fibonacci Number in Java
Problem
A number is a Fibonacci number exactly when 5×n²+4 or 5×n²−4 is a perfect square — a fact derived from the closed-form Fibonacci formula (Binet's formula).
Given a number, determine whether it is a Fibonacci number.
Java Program
public class FibonacciNumberCheck {
static boolean isPerfectSquare(long x) {
long root = (long) Math.sqrt(x);
// checks root+1 too, as a safety net against floating-point rounding in Math.sqrt()
return root * root == x || (root + 1) * (root + 1) == x;
}
public static void main(String[] args) {
int n = 8;
long test1 = 5L * n * n + 4;
long test2 = 5L * n * n - 4;
boolean isFibonacci = isPerfectSquare(test1) || isPerfectSquare(test2);
System.out.println(n + " is a Fibonacci number: " + isFibonacci);
}
}Output
Core Logic
Rather than generating Fibonacci numbers to check for a match, a known mathematical identity turns membership testing into a single perfect-square check.
- 1
isPerfectSquare(x)takes the integer square root ofxand checks whether squaring it back givesxexactly. - 2It actually checks both
rootandroot + 1squared, as a safety net againstMath.sqrt()'s floating-point rounding landing just under the true root. - 3
test1andtest2compute5n² + 4and5n² − 4. - 4
nis a Fibonacci number exactly when either of those two values is a perfect square.
8, 5 × 64 + 4 = 324, and 324 is 18² — a perfect square — so 8 is confirmed a Fibonacci number.Key Point: This is a genuine mathematical shortcut, not an approximation — the 5n²±4 identity is a proven necessary-and-sufficient test, so there's no need to generate a single Fibonacci number to use it.
Why: The perfect-square test runs in constant time regardless of n's magnitude, needing only a square root and a couple of multiplications.
Key Concepts
Approach 2: Generate and Check
public class FibonacciNumberCheckGenerate {
public static void main(String[] args) {
int n = 8;
int a = 0, b = 1;
boolean isFibonacci = (n == a);
while (b <= n) {
if (b == n) {
isFibonacci = true;
break; // found an exact match, no need to generate further
}
int next = a + b; // next term is the sum of the last two
a = b;
b = next;
}
System.out.println(n + " is a Fibonacci number: " + isFibonacci);
}
}
Output
Core Logic
A more direct approach just generates Fibonacci numbers up to n and checks whether n shows up among them — no special identity required.
- 1
aandbtrack two consecutive Fibonacci terms, starting at0and1. - 2The loop keeps generating the next term as long as
bhasn't yet passedn. - 3If
bever equalsnexactly,nis confirmed a Fibonacci number and the loop exits immediately. - 4If the loop finishes because
bpassednwithout ever matching it,nisn't a Fibonacci number.
0, 1, the terms generated are 1, 2, 3, 5, 8 — the fifth term generated lands exactly on 8, confirming the match.Key Point: Because Fibonacci numbers grow exponentially, this never needs many iterations even for a fairly large n — but the 5n²±4 identity still answers the question in a single step, with no generation at all.
Why: Fibonacci numbers grow exponentially, so only a logarithmic number of terms need to be generated before reaching or passing n.