Shell Script to Generate Fibonacci Series Iteratively and Recursively

By definition, the first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two. I will show you two shell script to generate Fibonacci series recursively and iteratively. Also you will get idea about the application of Fibonacci series/numbers.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2, with seed values F0 = 0 and F1 = 1.

Shell_Script_to_Generate_Fibonacci_Series_Iteratively_and_Recursively

What are the application or use of Fibonacci numbers or series?

The original inspiration for the Fibonacci numbers came from 2 rabbits. If you have 2 rabbits, they will breed and you will then have 3 rabbits. 3 rabbits breeds to make 5, 5 to make 8 and so on. The Fibonacci sequence.

This ratio is the basis many sculptors, artists, and architects over the years have used to create golden rectangles that are pleasing to the eye. It has also been used in designing many man-made structures and materials. Usage of the Fibonacci numbering sequence in design include the Parthenon, the United Nations building, music by Partok, and art by Van Gogh, Monet, Vermeer, Leonardo da Vinci and others.This sequence is not only found in man-made elements but also all around us in nature. This is a basis on which many Christian scientists determine to prove that there was a great and marvelous God who created our Earth and universe. It is not by mere coincidence that the Fibonacci sequence has an ubiquitous presence throughout creation.

Iterative Method in Shell Script:

#!/bin/bash
# SCRIPT:  interactive_fibonacci.sh
# USAGE:   interactive_fibonacci.sh [Number]
# PURPOSE: Generate Fibonacci sequence.
#
echo "---------------------------------------------------------------"
echo "Collected By: Fahad Ahammed"
echo "Website: www.obakfahad.com"
echo "Email: me@obakfahad.com"
echo "---------------------------------------------------------------"
#
#####################################################################
#                     Script Starts Here                            #
#####################################################################

if [ $# -eq 1 ]
then
Num=$1
else
echo -n "Enter a Number :"
read Num
fi

f1=0
f2=1

echo "The Fibonacci sequence for the number $Num is : "
echo ""

for (( i=0;i<=Num;i++ ))
do
echo -n "$f1 "
fn=$((f1+f2))
f1=$f2
f2=$fn
done

echo
echo ""
echo "Thank You"
echo ""

Output:

bash iterative_fibonacci.sh 14
---------------------------------------------------------------
Collected By: Fahad Ahammed
Website: www.obakfahad.com
Email: me@obakfahad.com
---------------------------------------------------------------
The Fibonacci sequence for the number 14 is :

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Thank You

Recursive Method in Shell Script:

#!/bin/bash
# SCRIPT:  recursive_fibonacci.sh
# USAGE:   recursive_fibonacci.sh [Number]
# PURPOSE: Generate Fibonacci sequence.
#
echo "---------------------------------------------------------------"
echo "Collected By: Fahad Ahammed"
echo "Website: www.obakfahad.com"
echo "Email: me@obakfahad.com"
echo "---------------------------------------------------------------"
#
#####################################################################
#                      Arguments Checking                           #
#####################################################################

if [ $# -eq 1 ]
then
Num=$1
else
echo -n "Enter a Number : "
read Num
fi

#####################################################################
#                      Define Functions Here                        #
#####################################################################

Fibonacci()
{

case $1 in
0|1) printf "$1 " ;;
*) echo -n "$(( $(Fibonacci $(($1-2)))+$(Fibonacci $(($1-1))) )) ";;
esac

#$(( )) construct is used instead of expr command for doing addition.
#$( ) constrict is used instead of back ticks.

}

#####################################################################
#                      Main Script Starts Here                      #
#####################################################################

echo "The Fibonacci sequence for the number $Num is : "
echo ""

for (( i=0; i<=$Num; i++ ))
do
Fibonacci $i                     #Calling function Fibonacci
done

echo
echo ""
echo "Thank You"
echo ""

Output:

bash recursive_fibonacci.sh 14
---------------------------------------------------------------
Collected By: Fahad Ahammed
Website: www.obakfahad.com
Email: me@obakfahad.com
---------------------------------------------------------------
The Fibonacci sequence for the number 14 is : 

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 

Thank You

Difference: Outputs are same but difference in appearing. You will find iterative results very quickly but recursive one will make delay and appear the numbers slowly.

If you use time command you will get clear idea.

Iterative with Time:

time bash iterative_fibonacci.sh 14
---------------------------------------------------------------
Collected By: Fahad Ahammed
Website: www.obakfahad.com
Email: me@obakfahad.com
---------------------------------------------------------------
The Fibonacci sequence for the number 14 is :

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Thank You


real    0m0.006s
user    0m0.000s
sys    0m0.004s

Recursive with Time:

time bash recursive_fibonacci.sh 14
---------------------------------------------------------------
Collected By: Fahad Ahammed
Website: www.obakfahad.com
Email: me@obakfahad.com
---------------------------------------------------------------
The Fibonacci sequence for the number 14 is :

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Thank You


real    0m3.085s
user    0m0.000s
sys    0m0.016s

How to use the Codes or Script?

You can just use terminal.

Create a bash file (.sh) :

touch iterative_fibonacci.sh

Then make that file executable.

chmod a+x iterative_fibonacci.sh

Then edit by nano.

nano iterative_fibonacci.sh

Paste the code and save or use any editor to save. And then you can execute.

How to Execute?

You can use bash,

bash iterative_fibonacci.sh 15

or just script,

./iterative_fibonacci.sh 15

If you have any question you can just ask here in comment section. Thank you.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: