Shell script to generate Fibonacci series using recursion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to generate Fibonacci series using recursion
# 1  
Old 12-08-2009
Shell script to generate Fibonacci series using recursion

I am facing problem with Shell script to generate Fibonacci series using recursion i.e. recursive function.
Here is my script:
Code:
#!/bin/sh

fibo()
{
    no=$1
    if [ $no -eq 1 ]; then
        return 0
    elif [ $no -eq 2 ]; then
        return 1
    else
        a1=`expr $no - 1`
        fibo $a1
        a=$(echo $?)

        b1=`expr $no + 1`
        fibo $b1
        b=$(echo $?)

        c=`expr $a + $b`
        return $c
    fi
}

####### Main

clear

echo -e "Enter number of terms : \c"
read n

if [ $n -gt 0 ]; then
    for (( i=1; i<=$n; i++ ))
    do
        fibo $i
        echo $?    
    done
else
    echo -e "Invalid input."
fi

But it does not give output.
Please help...
# 2  
Old 12-08-2009
I don't think you can call function within the function
# 3  
Old 12-08-2009
Quote:
Originally Posted by vidyadhar85
I don't think you can call function within the function
Thank you sir for your reply. I have a program to generate factorial of a number solved by recursion where I called the fact() function within fact().

---------- Post updated at 07:23 PM ---------- Previous update was at 01:31 PM ----------

At last it is solved.
Code:
#!/bin/sh

# Shell scrpt to generate Fibonacci series using recursion

export MINIDX=2                     

Fibonacci ()
{
    idx=$1                
    if [ "$idx" -lt "$MINIDX" ]; then
        echo "$idx"              
    else
        (( --idx ))              
        term1=$( Fibonacci $idx )       

        (( --idx ))              
        term2=$( Fibonacci $idx )       

        echo $(( term1 + term2 ))
    fi
}

echo -n "Enter the number of term : "
read MAXTERM

for (( i=0; i<=$MAXTERM; i++ ))
do                      
    FIBO=$(Fibonacci $i)
    echo -n "$FIBO "
done

echo

exit 0


Last edited by Tapas Bose; 12-08-2009 at 09:55 AM.. Reason: spelling mistake
# 4  
Old 12-08-2009
Good try, the code run very slow, can anyone optimize it?

Code:
$ ./your_script
Enter the number of term : 10
0 1 1 2 3 5 8 13 21 34 55


Last edited by rdcwayx; 12-08-2009 at 04:06 PM..
# 5  
Old 12-08-2009
I guess recursion is a requirement? It sure seems inefficient.

Your script using recursion:
Code:
$ time ./fibo2.sh
Enter the number of term : 10
0 1 1 2 3 5 8 13 21 34 55

real    0m14.875s
user    0m8.454s
sys     0m4.765s

Mine not using recursion:
Code:
$ time ./fibo.sh
Enter the number of term : 10
Fibonacci (10)=55

real    0m1.817s
user    0m0.046s
sys     0m0.030s

# 6  
Old 12-08-2009
Wrench

Another recursive one:
Code:
#!/bin/ksh

function Fibonacci {
  case $1 in
    0|1) printf "$1 " ;;
    *)   printf "$(( $(Fibonacci $(($1-2))) + $(Fibonacci $(($1-1))) )) ";;
  esac
}

for (( i=0; i<=$1; i++ )); do
  Fibonacci $i
done
echo

Code:
$ time ./test 10
0 1 1 2 3 5 8 13 21 34 55

real    0m0.086s
user    0m0.072s
sys     0m0.012s

(On a pentium III 850 MHz)

With the OP's script I get similar numbers if I leave out the read statement

Last edited by Scrutinizer; 12-08-2009 at 05:57 PM..
# 7  
Old 12-08-2009
Wow quite interesting results. Wonder if its some oddity because I am running this in cygwin. I am using a centrino2 vpro2

I changed the 3 scripts to use $1 instead of a read and the times required are vastly different.

Code:
None recursive script I threw together:
$ time ./fibo.sh 10
Fibonacci (10)=55

real    0m0.055s
user    0m0.046s
sys     0m0.030s

Original script from post #2:
$ time ./fibo2.sh 10
0 1 1 2 3 5 8 13 21 34 55

real    0m18.554s
user    0m8.199s
sys     0m4.926s

Script posted by scrutinizer in post #6, well I had to slightly change your while loop cause my shell 
could not digest the inline declaration of "i" so mine looks like this at the bottom of the file:
while [ $i -lt $1 ]
do
let i=i+1
  Fibonacci $i
done
echo

$ time ./fibo3.sh 10
1 1 2 3 5 8 13 21 34 55

real    0m11.508s
user    0m7.953s
sys     0m4.610s

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script newbie- how to generate service log from shell script

Hi, I am totally a newbie to any programming languages and I just started an entry level job in an IT company. One of my recent tasks is to create a script that is able to show the log file of linux service (i.e. ntpd service) lets say, if I run my script ./test.sh, the output should be... (3 Replies)
Discussion started by: xiaogeji
3 Replies

2. Shell Programming and Scripting

Generate documentation for a shell script

Hi, I've written a shell script with proper intentation and commenting structure. However, I would like to generate documentation for the shell which I have written. Is there any tool as such to generate it like we have javagen/docgen ? Please help. Thanks, Arjun (0 Replies)
Discussion started by: arjun_arippa
0 Replies

3. Shell Programming and Scripting

Fibonacci series -going into infinite loop

Hello, I am a beginner to shell programming. Coded the following for Fibonacci series. #!/bin/bash fib() { i=0 j=1 arr=0 arr=1 echo "enter the limit:" read n while do fo= expr $j - 1 f1=$j f2= expr $j + 1 arr= expr ${arr} + ${arr} echo ${arr} (3 Replies)
Discussion started by: Rookie222
3 Replies

4. Shell Programming and Scripting

generate logfile in a shell script

Unix Gurus, I have a shell script which has few "echo" statements. I am trying to create a logfile where all the outputs of the echo statement sare stored. I will have to add this as the final step in the existing script so that everytime the script runs, a logfile is generated with all the... (1 Reply)
Discussion started by: shankar1dada
1 Replies

5. Homework & Coursework Questions

Help with shell script to find sum of first n numbers of Fibonacci series

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Shell script to find sum of first n numbers of Fibonacci series 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: Kshitija
0 Replies

6. Shell Programming and Scripting

Shell script to find the sum of first n Fibonacci numbers

pls give me the solution for this i need it for my exam pls pls pls Shell script to find the sum of first n Fibonacci numbers (1 Reply)
Discussion started by: Kshitija
1 Replies

7. Shell Programming and Scripting

problem in fibonacci series

hi, I'm a beginner to UNIX and got some problem in this fibonacci.Please help me out.Here is the code: fibo() { if then fibo=` expr {fibo ($1 - 2)} + {fibo ($1 - 1)}` | bc echo $fibo fi } echo "enter a number:" read x #echo "The fibonnacci series for value $x is:" fibo $x ... (4 Replies)
Discussion started by: janani_kalyan
4 Replies

8. Shell Programming and Scripting

How to generate a series of numbers

Hi All, I have a requirement where in I have an input as follows:- input=1-4,6,8-10,12-15 I need to explode this range into an output file as follows:- 1 2 3 4 6 8 9 10 12 13 14 15 My input may vary like 1,5-9,11-13,15-17....... (3 Replies)
Discussion started by: rony_daniel
3 Replies

9. UNIX for Dummies Questions & Answers

generate xml from a shell script

Hello! I would like to generate an xml file from the output of various commands generated from within a shell script (some will be in CDATA). At the moment the only solution I have come up with is echoing xml tags around the commands eg. echo "<bitism>" >> outputfile /usr/sbin/prtconf... (1 Reply)
Discussion started by: speedieB
1 Replies

10. Shell Programming and Scripting

Fibonacci series

Need code to run the Fibonacci series from 0 to 10 (16 Replies)
Discussion started by: nycol
16 Replies
Login or Register to Ask a Question