Given a number ‘n’, how to check if n is a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ..
Input : 8 Output : Yes Input : 34 Output : Yes Input : 41 Output : No
A simple way is to generate Fibonacci numbers until the generated number is greater than or equal to ‘n’. Following is an interesting property about Fibonacci numbers that can also be used to check if a given number is Fibonacci or not.
A number is Fibonacci if and only if one or both of (5*n2 + 4) or (5*n2 – 4) is a perfect square (Source: Wiki). Following is a simple program based on this concept.
1 is a Fibonacci Number 2 is a Fibonacci Number 3 is a Fibonacci Number 4 is a not Fibonacci Number 5 is a Fibonacci Number 6 is a not Fibonacci Number 7 is a not Fibonacci Number 8 is a Fibonacci Number 9 is a not Fibonacci Number 10 is a not Fibonacci Number
This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- An efficient way to check whether n-th Fibonacci number is multiple of 10
- Check if a number is Full Fibonacci or not
- Check if the n-th term is odd or even in a Fibonacci like sequence
- Check whether frequency of characters in a string makes Fibonacci Sequence
- Check whether Array represents a Fibonacci Series or not
- Find Index of given fibonacci number in constant time
- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Count of Fibonacci divisors of a given number
- Count nodes in the given tree whose weight is a fibonacci number
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Find n terms of Fibonacci type series with given first two terms
- Find the GCD of N Fibonacci Numbers with given Indices
- Find the longest Fibonacci-like subarray of the given array
- Count of consecutive Fibonacci pairs in the given Array
- Count of Fibonacci pairs which satisfy the given equation
- Remove all the fibonacci numbers from the given array
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
Improved By : Mithun Kumar