Test if a number is a Fibonacci number
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Testing whether a number belongs to the Fibonacci sequence is a classic problem in number theory and algorithm design. The sequence starts with 0 and 1, and every next term is the sum of the previous two terms. In real software, you usually want a method that is both mathematically correct and efficient for large inputs.
Method 1: Perfect-Square Test
A non-negative integer n is Fibonacci if and only if at least one of these values is a perfect square:
5 * n * n + 45 * n * n - 4
This gives an O(1) arithmetic test, ignoring integer bit complexity.
This is usually the best general-purpose check in Python because math.isqrt works with arbitrarily large integers.
Method 2: Iterative Generation
If you want a simple logic path without number theory identities, generate Fibonacci terms until you reach or pass n.
This method is O(k) where k is the index of the closest Fibonacci term, so runtime grows with n. It is still fast for many practical values and is easy to reason about in interviews or teaching.
Method 3: Membership in a Precomputed Set
If you need repeated lookups within a bounded range, precompute once and use set membership.
This approach trades memory for speed. After precomputation, checks are effectively constant time.
Choosing the Right Approach
Use the perfect-square method when:
- You need a one-off check.
- Inputs can be very large.
- You want concise and mathematically strong logic.
Use iterative generation when:
- You need maximum readability.
- Input values are moderate.
- You do not want to rely on a formula.
Use precomputed sets when:
- You perform many checks in the same bounded range.
- You can afford memory for a lookup table.
JavaScript Version
JavaScript number precision can be tricky for large integers. Use BigInt for safer behavior.
This avoids floating-point rounding issues that appear if you use Math.sqrt on large values.
Common Pitfalls
- Forgetting that negative numbers are not part of the standard Fibonacci sequence in most programming contexts.
- Using floating-point square root checks on large integers and getting false results due to precision limits.
- Assuming iterative generation is constant time for very large inputs.
- Precomputing a lookup set without enforcing an upper limit, leading to unnecessary memory use.
- Confusing sequence index with sequence value, especially when handling base cases
0and1.
Summary
- The perfect-square identity provides a robust and efficient Fibonacci membership test.
- Iterative generation is simple and reliable for moderate input sizes.
- Precomputed sets are ideal for repeated checks in bounded ranges.
- For JavaScript, prefer
BigIntimplementations for high numeric accuracy. - Handle edge cases explicitly, especially negative inputs and the two base values.
Related reading
- Test if point is in some rectangle
- Testing for repeated characters in a string
- Tetris-ing an array
- Tetris Piece Rotation Algorithm
- tf.self_adjoint_eig fails for covariance matrix
- The amortized complexity of stdnext_permutation?
- Text clustering within a log file
- Text, string-based chord recognition algorithms?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.