If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our **Contribution Guidelines**.

# How to Test If a Number is a Fibonacci Number

Last modified: November 5, 2022

## 1. Overview

In this tutorial, **we’ll discuss the Fibonacci series**. We’ll define the Fibonacci series and explore various methods to generate Fibonacci numbers with examples.

Finally, **we’ll present the pseudocode to check if a given number is a Fibonacci number or not.**

## 2. Fibonacci Series

The Fibonacci series can be defined as the sequence of numbers: . The Fibonacci series was initially elaborated and used in Indian mathematics by Pingala. However, it was illustrated to the world by Leonardo Pisano Bogollo, later known as Fibonacci.

Additionally, the Fibonacci series follows an interesting property. The property is **we can find the next element in the series by adding two elements that appear before it:**

Let’s verify the property. Element 34 can be found by adding two previous elements, 21 and 13 (21 + 13 = 31). Similarly, we can find element 8 by adding two previous elements, 5 and 3. Additionally, we can find the next element of 34 in the series. It’ll be 55 (34 + 21).

Now, let’s derive a generalized equation for generating the Fibonacci series. Initially, it starts with initial data and , which are given in advance. Moreover, the series proceeds further by adding two previous adjacent elements. Hence, the element at the position in the series can be calculated as:

Additionally, the numbers that are part of the Fibonacci series are known as the Fibonacci numbers. Hence, the Fibonacci series helps us to verify if a given number is a Fibonacci number or not.

## 3. Binet’s Formula

Besides the general property, there’s another way to find the elements in the Fibonacci sequence. **We can generate the elements in the Fibonacci series using the golden ratio method.** Additionally, it’s popularly known as Binet’s formula. Therefore, the equation to find the element in the series is:

The variable represents the golden ratio. Additionally, the variable is the conjugate of . Therefore, we can express in form of :

Therefore, we can simplify Binet’s formula:

Moreover, the golden ration can be expressed as:

Hence, the final equation is:

Now, let’s verify this formula. Therefore, we’ll calculate some elements from the Fibonacci series and compare them with the Fibonacci series presented in the previous section. Let’s check for 5:

Next, let’s set to 6:

Finally, we set 7:

Hence, as we can see using Binet’s formula, we can generate the elements of the Fibonacci series.

## 4. Approach for Checking a Fibonacci Number

As of now, we know how to generate the elements in the Fibonacci series. Now let’s discuss how to check whether a given number is a Fibonacci number or not. First, let’s look at the flowchart:

Initially, we input a number to start the checking process. In the next step, we compute two variables and :

**Additionally, we check if any of them is a perfect square or not.** In mathematics, a number is a perfect square if we can express it as a product of two equal integers. A number is a perfect square if and .

Therefore, in order to check if or is a perfect square or not, we compute and . Finally, the given number is a Fibonacci number, if at least one of or is a perfect square.

## 5. Pseudocode

Now let’s take a look at the pseudocode:

Initially, we take a given number as an input, and the pseudocode returns whether the number is a Fibonacci or not. Let’s analyze the time complexity of the pseudocode. In this pseudocode, we can only input a single number. Hence, both time and space complexity are . Moreover, if we want to input an array and t**he total number of elements in the array is ****, the time complexity would be** .

## 6. Validation of Pseudocode

**Let’s verify the pseudocode with some examples.** Let’s take as input and run the pseudocode:

In this example as we can see . Hence, the input number 5 is a Fibonacci number. Let’s take another example. In this case, the given input number is 7. The number 7 isn’t part of the Fibonacci series. Hence, it’s not a Fibonacci number. Let’s run the pseudocode and verify it:

Here, neither nor . Both 249 and 241 are not perfect squares. **Therefore, the given input **** is not a Fibonacci number.**

## 7. Applications

**The Fibonacci series and numbers are used in several applications in the field of computer science and mathematics.** In computer science, the Fibonacci numbers are used in lossy compression in audio file format, binary tree representation, Fibonacci heap data structure, Fibonacci cube graph, sorting algorithms, the golden-section search, pseudorandom number generators.

In the mathematics field, many important applications, including optimization method, obtaining the common divisors, number game prediction, dynamic integer generation, use the Fibonacci numbers.

Other fields like high energy physics, quantum mechanics, cryptography utilize the Fibonacci series in various applications.

## 8. Summary

In this tutorial, we discussed the Fibonacci series and numbers. We talked about two methods to generate the Fibonacci numbers in the Fibonacci series with numerical examples.

Finally, we presented pseudocode for testing a given number is a Fibonacci number or not. We validated the pseudocode with examples.