problem in fibonacci series


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem in fibonacci series
# 1  
Old 02-05-2009
Data 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 [ "$1" -gt "1" ]
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


And i'm getting the error as:
ksh fibo.sh
enter a number:
4
fibo.sh[4]: syntax error at line 1 : `(' unexpected

plese help me out.
Thanks in advance.

Janani
# 2  
Old 02-05-2009
I suppose the point was to try and learn recursion in shell programming...
But why????

IMHO -- recursion is rarely necessary, and typically overly complex.

To that end, here is a non-recursive solution,
following the algorithm from: Wiki_Fibonacci_series

Code:
fibo()
{
num=$1

if [ $num -eq 0 ]; then
  print 0
  return
fi

if [ $num -eq 1 ]; then
  print 0, 1
  return
fi

print -n 0, 1

first=0
second=1

counter=2
while [ $counter -lt $num ]; do

  curr_total=$(( first + second ))
  print -n ", $curr_total"

  first=$second
  second=$curr_total

  counter=$(( counter + 1 ))

done

print

return
}





echo "enter a number: "
read x
echo "The fibonnacci series for value $x is:"
fibo $x

# 3  
Old 02-05-2009
Quote:
Originally Posted by quirkasaurus
Code:
..
while [ $counter -lt $num ]; do
..

Should be less or equal Smilie
# 4  
Old 02-05-2009

Code:
fibo()
{
  num=${1:-100}
  n2=1
  n1=0
  while n=$(( $n1 + $n2 )); [ $n -le $num ]
  do
    printf "%d " "$n"
    n1=$n2
    n2=$n
  done
  echo
}

## The loop continues until the user enters a valid number
while :
do
  printf "Enter a number: "
  read number
  case $number in
    *[!0-9]* | "") printf "\a*** Invalid number ***\n" >&2 ;;
    *) break ;;
  esac
done
fibo "$number"

# 5  
Old 02-05-2009
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Homework & Coursework Questions

program to find and print a Fibonacci sequence of numbers. --Errors

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: I am trying to convert a C language program over to Sparc Assembley and I am getting Undefined first referenced... (4 Replies)
Discussion started by: kenjiro310
4 Replies

3. Programming

Fibonacci (assembly)

When i run this with gcc filename.s -o filename. It is giving fatal error: Unknown opcode at define registers. Anyhelp will be appreciated .section ".data" prompt: .asciz "\nThis program prints the Fibonacci sequence" prompt2: .asciz "\nEnter a limit on the largest number to be displayed:"... (2 Replies)
Discussion started by: Learnerabc
2 Replies

4. UNIX for Dummies Questions & Answers

Why does fibonacci sequence script stop making sense at 92nd iteration?

So, Just for practice, I wrote a simple fibonacci sequence script in bash. (03:08:02\$ cat fib #!/usr/bin/bash ret () { echo -ne "\n" sleep .5 } a=1 b=2 echo -n $a #1 A ret echo -n $b #2 B ret (8 Replies)
Discussion started by: DeCoTwc
8 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

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: #!/bin/sh fibo() { no=$1 if ; then return 0 elif ; then return 1 else a1=`expr $no - 1` fibo $a1 ... (10 Replies)
Discussion started by: Tapas Bose
10 Replies

8. Programming

C with MQ Series

Hi, Any one please let me know, how to write in to MQ Series through C. I tried, but it was vein, am expecting material(URL) or code. Thanks, Naga:cool: (5 Replies)
Discussion started by: Nagapandi
5 Replies

9. AIX

FTP Speed Problem on IBM P-Series equiped with AIX 5.2

Hi, We have IBM P-Series servers (P690, P650) equiped with AIX 5.2. Further we have 10/100 MB ethernet cards in P650 and 10/100/1000 MB ethernet cards in P690 servers. Servers are on a LAN connected with Cisco 3750 catalyst switch. FTP from one server to another is very slow,,,even 3-4 MB... (3 Replies)
Discussion started by: aqeelcu@hotmail
3 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