` By definition in mathematics, the Fibonacci Numbers are the numbers`

in the below sequence:

0,1,1,2,3,5,8,13,21,34,55,89,144, ......

By definition, the first two Fibonacci numbers are 0 and 1, and each

subsequent number is the sum of the previous two. Some sources omit

the initial 0, instead beginning the sequence with two 1s.

In mathematical terms, the sequence Fn of Fibonacci numbers is defi-

ned by the recurrence relation

Fn = Fn-1 + Fn-2,

with seed values

F0 = 0 and F1 = 1.

#### Iterative Method:

#!/bin/bash#!/bin/bash

# SCRIPT: fibo_iterative.sh

# USAGE: fibo_iterative.sh [Number]

# PURPOSE: Generate Fibonacci sequence.

# \\\\ ////

# \\ - - //

# @ @

# ---oOOo-( )-oOOo---

#

#####################################################################

# 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 : "

for (( i=0;i<=Num;i++ ))

do

echo -n "$f1 "

fn=$((f1+f2))

f1=$f2

f2=$fn

done

echo

**OUTPUT:**

# sh fibo_iterative.sh 18

The Fibonacci sequence for the number 18 is :

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584

# sh fibo_iterative.sh

Enter a Number :20

The Fibonacci sequence for the number 20 is :

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

#### Recursive Method:

#!/bin/bash

# SCRIPT: fibo_recursive.sh

# USAGE: fibo_recursive.sh [Number]

# PURPOSE: Generate Fibonacci sequence.

# \\\\ ////

# \\ - - //

# @ @

# ---oOOo-( )-oOOo---

#

#####################################################################

# 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 : "

for (( i=0; i<=$Num; i++ ))

do

Fibonacci $i #Calling function Fibonacci

done

echo

**OUTPUT:**

# sh fibo_recursive.sh

Enter a Number : 11

The Fibonacci sequence for the number 11 is :

0 1 1 2 3 5 8 13 21 34 55 89

# sh fibo_recursive.sh 13

The Fibonacci sequence for the number 13 is :

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

Be aware that recursion is resource-intensive and executes slowly,

and is therefore generally not appropriate to use in a script.

[root@localhost shell]# time ./fibo_iterative.sh 15

The Fibonacci sequence for the number 15 is :

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

real 0m0.008s

user 0m0.008s

sys 0m0.000s

[root@localhost shell]# time ./fibo_recursive.sh 15

The Fibonacci sequence for the number 15 is :

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

real 0m7.875s

user 0m0.908s

sys 0m5.188s

Too many levels of recursion may crash a script with a segfault.

## Wednesday, December 22, 2010

Posted by venu k

13 comments | 12:38 PM

Subscribe to:
Post Comments (Atom)

Hi,

ReplyDeleteThanks for sharing. Just to share my own version too:

fibo() { local a=0; b="$1"; inc="$2"; while ((b < $inc)); do tmp=$((a+b)); a=$b; b="$tmp"; printf '%s ' "$tmp"; done; }; fibo 34 2000

Regards,

Valentin

Enter a Number :4

ReplyDeleteThe Fibonacci sequence for the number 4 is :

fib.sh: 27: fib.sh: Syntax error: Bad for loop variable

Copy/paste from here, no errors.

Shame on you!

ReplyDeletea=1;b=1;for i in `seq 91`;do echo $((x=b,b=a+b,a=x));done

That's all, folks!

Same thing, if you want to print also first numbers of sequence:

ReplyDeleteecho $((b=1,a=0));for i in `seq 92`;do echo $((x=b,b=a+b,a=x));done

Prints out Fibonacci numbers

ReplyDelete#include

int main()

{

int old_number= 1;

int current_number = 1;

int next_number;

while (current_number < 200) {

printf("%d\n", current_number);

next_number = current_number + old_number;

old_number = current_number;

current_number = next_number;

}

return (0);

}

To include commas in the output:

ReplyDeleteecho -n "$f1 " | sed -r ':L;s=\b([0-9]+)([0-9]{3})\b=\1,\2=g;t L'

The future of automation testing tool is on the positive tool. As the demand of quality web application keeps on increasing, the tool like Selenium IDE is getting very popular all over the world. Thus, taking Selenium Training in Chennai will help you to enter software testing industry. Best Selenium training institute in Chennai

ReplyDeleteThanks for your article. Your post helped me to understand the future of QTP automation testing tool. Keep on your blog with awesome information. Best QTP Course in Chennai | QTP training

ReplyDeleteThe future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers. software testing course in Chennai | Software Testing Training in Chennai | Software testing course in Chennai

ReplyDeleteThe future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers.

ReplyDeleteRegards,

Software testing training in chennai|testing training chennai|Software testing institutes in chennai

Individual is expected to choose properly the right Selenium software regarding his/her desires.

ReplyDeleteSelenium IDE is allocated as a Firefox plug in. It's better to install and use. Consumer isn't needed to get previous programming information. Selenium IDE can be an ideal tool for a unknowing person.

Selenium RC is actually a host which allows user to generate test programs in a preferred programming language. It also enables undertaking test programs within the huge spectrum of browsers . For more info on this check this link

The tool was later on commended together with the brand “Selenium Core”.

ReplyDeleteSelenium IDE (Selenium Integral Development Environment)

Selenium IDE was created by Shinya Kasatani. Although studying Selenium Central, he understood that this JavaScript signal may be lengthy to generate an integrated development environment (IDE) which can be plugged into Mozilla Firefox . For more info on this see this link

Đơn vị vận chuyển gửi hàng từ việt nam sang Hoa Kỳ

ReplyDeleteCông ty vận chuyển gửi hàng từ việt nam sang Lôn-đôn chi phí rẻ

Đơn vị ship nhanh hàng hóa từ việt nam sang thái lan chi phí thấp