Fibonacci series

Need code to run the Fibonacci series from 0 to 10

.... now that we are all aware of your 'needs'..... what are your 'deeds'?

What are you on about

maybe I wasn't clear.....
could explain what you've tried and what exactly you're having problem with?

Homework, me thinks?

Maybe just a rabbit breeder.

Actually it was a problem I was trying to solve using a do while loop at age 42 I don't think it's homework

Ruby:

def fib( n )
  a, b = 0, 1
  n.times { a, b = b, a + b }
  b
end

9.times {|i| print i, ' ', fib(i), "\n" }

Hi,
you can try this script.i admit calculation part in while loop could be reduced to minimum.but this script will work.just adjust else part i have not considered if no of terms are less than 2.
this script will generate series upto the terms entered..

#!/usr/bin/ksh
typeset -u f[50]
f[0]=1
f[1]=1
echo "enter no of terms"
read n
echo "fibonaaci series upto $n terms"
if [ $n -gt 2 ]
then
i=2
echo ${f[0]}
echo ${f[1]}
while [ $i -lt n ]
do
k=`expr $i - 1`
j=`expr $i - 2`
a=${f[k]}
b=${f[j]}
f[i]=`expr $a + $b`
echo ${f[i]}
i=`expr $i + 1`
done
else
echo ${f[0]}
fi

try this

#!/usr/bin/ksh
typeset -i f[50]
f[0]=1
f[1]=1
echo "enter no of terms"
read n
echo "fibonaaci series upto $n terms"
if [ $n -gt 2 ]
then
i=2
echo ${f[0]}
echo ${f[1]}
while [ $i -lt n ]
do
f[i]=`expr ${f[i-1]}+${f[i-2]}`
echo ${f[i]}
i=`expr $i + 1`
done
else

echo ${f[0]}
echo ${f[1]}
fi

Thanx for the solutions trying them out tonight, then i'll try to figure out how they work. Just new to Unix and i'm trying to learn it because it seems so interesting but complicated at times, so once again thanx for your time and help

Dear All,

   Am got febonaaci script example from our Fourm. While i was executing always am getting this "integer Expression Expected Error".

   There is a Statement in that Script 

while [ $i -lt n ] ----> always the error messge showed to this statement
do
k=`expr $i - 1`
j=`expr $i - 2`
a=${f[k]}
b=${f[j]}
f[i]=`expr $a + $b`
echo ${f[i]}
i=`expr $i + 1`
done

    Can Any body Help me to Learn this kind of Issue.

Thank You

Nirmal Babu

[quote="n
while [ $i -lt n "]
----> always the error messge showed to this statement
[/quote]

Did you try $n??

i=1
a=0
b=1
echo ${a},
while [ $i != 10 ]
do
b=`expr $a + $b`
echo ${b},
c=$a
a=$b
b=$c
i=`expr $i + 1`
done

Not sure about the simplest, but probably the smallest, yes! Here you go:

$cat fib

#!/bin/ksh
if [ $1 -gt 2 ]
then
echo `expr $2 + $3`
fib `expr $1 - 1` $3 `expr $2 + $3`
fi

$fib 10 0 1
1
2
3
5
8
13
21
34

Note above that the script is called with three arguments: no. of terms required, the strating fibionacci term, the next fibionacci term

Hi Klashxx,

Yup.... it is working, when i changed to 'n' to '$n'. Thz a lot.. :))

I Would like to thank for everyone suggestion to me. Thank You Very Much.

Nirmal Babu

#!/bin/sh

febonacci() {

choice=$1
n1=0
n2=1
i=1

while [ $i -le $choice ]
do
if [ $i -eq 1 ]
then
echo "$n1\t \c"
elif [ $i -eq 2 ]
then
echo "$n2\t \c"
else
new=`expr $n1 + $n2`
echo "$new\t \c"
n1=$n2
n2=$new
fi
i=`expr $i + 1`
done

}

##############################
# Main() #
##############################

echo "Enter the number of terms( 1-92 ) : \c"
read num
echo "Febonacci Series for $num terms"
febonacci $num
echo