Iterative Fibonacci Number Checking
This approach involves iterating through generating Fibonacci numbers until we either find a match with the given number or reach a Fibonacci number greater than the given number.
Example: To check if a number is a Fibonacci number using iterations in Javascript.
Javascript
function isFibonacci(N) { if (N == 0 || N == 1) { return true ; } let a = 0, b = 1, c; while ( true ) { c = a + b; a = b; b = c; if (c == N) { return true ; } else if (c >= N) { return false ; } } } let i = 8; if (isFibonacci(i)) { console.log(i + " is a Fibonacci number." ); } else { console.log(i + " is not a Fibonacci number." ); } |
Output
8 is a Fibonacci number.
How to Check if a Given Number is Fibonacci Number in JavaScript ?
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. Checking for Fibonacci numbers involves verifying whether a given number appears in this sequence. The first few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and 89, and so on. There are various approaches to check if a number is a Fibonacci number in Javascript or not which are as follows:
Table of Content
- Iterative Fibonacci Number Checking
- Using the Golden Ratio Formula
- Iterative On-the-Fly Fibonacci Generation