• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Wednesday, December 22, 2010

Posted by venu k
13 comments | 12:38 PM
  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.


13 comments:

  1. Hi,

    Thanks 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

    ReplyDelete
  2. Enter a Number :4
    The Fibonacci sequence for the number 4 is :
    fib.sh: 27: fib.sh: Syntax error: Bad for loop variable

    Copy/paste from here, no errors.

    ReplyDelete
  3. Shame on you!

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

    That's all, folks!

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

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

    ReplyDelete
  5. Prints out Fibonacci numbers

    #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);
    }

    ReplyDelete
  6. To include commas in the output:

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

    ReplyDelete
  7. 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

    ReplyDelete
  8. Thanks 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

    ReplyDelete
  9. The 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

    ReplyDelete
  10. The future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers.
    Regards,
    Software testing training in chennai|testing training chennai|Software testing institutes in chennai

    ReplyDelete
  11. Individual is expected to choose properly the right Selenium software regarding his/her desires.
    Selenium 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

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

    Selenium 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

    ReplyDelete