Things we will discuss in this Article:

  • What are Fibonacci (Fee-bo-na-chi) Series?
  • C++ Code Implementation to find Fibonacci Numbers.
  • Time Complexity Analysis.
  • Practice Problem for you to Strengthen this Concept.


  • Recursion.
  • Dynamic Programming.

Fibonacci Series:

Fibonacci Series is a naturally occurring series of numbers that leads us to various forms of design patterns accepted by nature.  Fibonacci Series looks something like: 0,1,1,2,3,5,8,13,21,34,…and hence, defined as:

    \[F_n=\left\{\begin{matrix} 0 & :n=0\\ 1 & :n=1\\ F_{n-1}+F_{n-2} & :\forall\ n\ \epsilon\ \mathbb{Z}-\left { 0, 1 \right } \end{matrix}\right.\]

and are the reason for the shapes of tornados, galaxies, sunflower seeds pattern, even your beauty(through golden ratio), and many more.

Recursive implementation to compute Fibonacci Number:

#include <bits/stdc++.h>
using namespace std;

// function to return fibonacci number
int fib(int n) {
    if (n <= 1) return n;
    // recursive call
    return fib(n - 1) + fib(n - 2);

int main() {
    int n;
    cout << "Enter n ";
    cin >> n;
    cout << endl << "Fibonacci number is " << fib(n) << endl;
    return 0;

DP implementation to compute Fibonacci Numbers:

Why use DP?

  • Dynamic Programming is used with recursive methods to speed the successive computation by saving the results from subproblem computation.
  • Secondly, we don’t go through the recursive tree all over again with all the computation, we can just search and use the saved results.

How we implemented DP here?

  • We implemented a ‘lookup’ array to store the computed values of subproblems to be used in the successive computation.
  • We used negative one(-1) as the initialization value for the lookup array, one must choose that value in DP as an initialization value that can never be reached in the computation process being performed.
  • We search and update our lookup array with each process of the subproblem.
#include <bits/stdc++.h>
using namespace std;
#define NIL -1
#define MAX 100

int lookup[MAX];

// initialising lookup array
void _init() {
    int i;
    for (i = 0; i < MAX; i++) lookup[i] = NIL;

// function to find and store fibonacci numbers
int fib(int n) {
    if (lookup[n] == NIL) {
        if (n <= 1)
            // storing unsaved value
            lookup[n] = n;
            // recursicve call and storing unsaved value
            lookup[n] = fib(n - 1) + fib(n - 2);

    return lookup[n];

int main() {
    int n;
    cout << "Enter n ";
    cin >> n;
    cout << endl << "Fibonacci number is " << fib(n) << endl;
    return 0;

Properties of Fibonacci Numbers:

Zeckendorf’s Theorem: Every positive integer can be represented in a unique combination of non-consecutive Fibonacci Numbers.
For any integer n >0, we have:

    \[\forall\ i\ \epsilon\ \mathbb{N},\ k_i\geq 2\]

    \[k_{i+1}> k_i+1\]

    \[n=\sum_{i=0}^{j}F_{k_i},\ F_k\ is\ Fibonacci\ Number.\]

    \[\forall n,\ \left \langle k_i \right \rangle\ is\ different.\]

Note:  Zeckendorf’s Theorem is the most asked Fibonacci Number’s property in competitive programming. Zeckendorf representations have applications in coding; they have been proposed for use in self-delimiting codes and run-length-limited binary codes.    

Algorithm for finding Zeckendorf’s Pattern(using Greedy Algorithm):

            1. Let n be the number for which we want to find Zeckendorf’s Pattern.
            2. Find the largest Fibonacci number k less than or equal to n.
            3. Replace n = n-k
            4. Repeat until we find the whole pattern i.e. until n becomes 0

Code implementation for Zeckendorf’s Pattern:

#include <bits/stdc++.h>
using namespace std;

// function to find largest fibonacci number smaller than n
int fib_search(int n) {
    if (n == 0 || n == 1) return n;

    int f1 = 0, f2 = 1, f3 = 1;
    while (f3 <= n) {
        f1 = f2;
        f2 = f3;
        f3 = f1 + f2;
    return f2;

// function to display the found fibonacci number
void display_fib(int n) {
    while (n > 0) {
        int f = fib_search(n);
        cout << f << " ";
        // updating n
        n = n - f;

int main() {
    int n;
    cout << "Enter the number n ";
    cin >> n;
    cout << endl << "Zeckendorf's Representation of " << n << " is \n";
    return 0;

Vajda’s Identity: It states that:

    \[F_{n+i}F_{n+j}-F_{n}F_{n+i+j}=\left ( -1 \right )^{n}F_{i}F_{j}\]

Catalan’s Identity: Catalan’s Identity is a special case of Vajda’s Identity.

    \[F_{n}^{2}-F_{n-r}F_{n+r}=\left ( -1 \right )^{n-r}F_{r}^{2}\]

Cassini’s Identity: 
Cassini’s Identity is a special case of Catalan’s Identity and henceforth child of Vajda’s Identity too. 

    \[F_{n-1}F_{n+1}-F_{n}^{2}=\left ( -1 \right )^{n}\]

GCD of Fibonacci Numbers: GCD of any two Fibonacci Numbers is equal to the Fibonacci number at the index of gcd of their indices:

    \[\forall m, n \ \epsilon\ \mathbb{Z}\left(>2 \right ) : gcd\left ( F_m, F_n \right ) = F_{gcd\left ( m,n \right )}\]

gcd: Greatest Common Divisor
Divisibility of Fibonacci Numbers:

    \[\forall m, n \ \epsilon\ \mathbb{Z}\left(>2 \right ) : m\mid n\Leftrightarrow F_m\mid F_n\]

 a | b is read as ‘a divides b’ (i.e. without leaving any remainder).

Sum of squares of Fibonacci Numbers: It is naturally the most intriguing property of Fibonacci Numbers. As it is this property that shows us the spiral behavior of the Fibonacci Numbers.

    \[F_1^2+F_2^2+F_3^2+\cdots +F_n^2=F_nF_{n+1}\]

Sequential relations for Fibonacci Numbers:

Sum of the sequence of Fibonacci Numbers:

    \[\forall m, n \ \epsilon\ \mathbb{Z}\left(\geqslant0 \right) : \sum_{j=0}^{n}F_j=F_{n+2}-1\]

Sum of even indexed Fibonacci Numbers:

    \[\forall n\geqslant 1: \sum_{j=1}^{n}F_{2j}=F_2+F_4+F_6+\cdots +F_{2n}=F_{2n+1}-1\]

Sum of odd indexed Fibonacci Numbers:

    \[\forall n\geqslant 1: \sum_{j=1}^{n}F_{2j-1}=F_1+F_3+F_5+\cdots +F_{2n-1}=F_{2n}\]

Sum of even sequence of products of Fibonacci Numbers:


Sum of the odd sequence of products of Fibonacci Numbers:


Methods to compute Fibonacci Number:

Euler-Binet Formula:

    \[F_n=\frac{{\Phi^n-\left ( 1-\Phi \right)^n}}{\sqrt{5}}=\frac{\Phi^n-\left ( -1/\Phi \right )^n}{\sqrt{5}}\]

\Phi is the golden ratio \left ( \Phi=\frac{1+\sqrt{5}}{2}=1.618 \right )

    \[F_n=\frac{\left (\frac{1+\sqrt{5}}{2} \right )^n - \left (\frac{1-\sqrt{5}}{2} \right )^n}{\sqrt{5}}\]

As the second term in the above expression is always less than 1, so we can drop that term, and henceforth the expressions reduce to:

    \[F_n=\left [\frac{\left (\frac{1+\sqrt{5}}{2} \right )^n}{\sqrt{5}} \right ]\]

and hence the Fibonacci number will be the closest integer to the obtained value.
Time Complexity: O(1)

Note: Binet’s relation can easily be proved using induction.

Fibonacci Number as the sum of Binomial Coefficients:

    \[\forall n\ \epsilon \ \mathbb{Z}(> 0): F_n=\sum_{k=0}^{\left \lfloor \frac{n-1}{2} \right \rfloor}\binom{n-k-1}{k}=\binom{n-1}{0}+\binom{n-2}{1}+\binom{n-3}{2}+\cdots +\binom{n-j}{j-1}+\binom{n-j-1}{j}\]

where j=\left \lfloor \frac{n-1}{2} \right \rfloor

Fibonacci Number as the sum of the
previous series:

    \[\forall n\geq 2, F_n=1+\sum_{i=0}^{n-2}F_i\]

Note:  This relation can be proved by induction.

Related Practice Problems:

Related Articles:


In this article, we learned about Fibonacci Numbers and Series and some of their most basic properties. And, we also learned how to find Fibonacci Numbers and Zeckendorf’s Pattern using Recursion and Dynamic Programming and Greedy Algorithm respectively.

Related topic which you may want to Read:

This is all for this article, if you have any doubt or there is an error in the content then do comment for which we will be thankful to you.

Thank You for Reading

Leave a Reply