Re: Fibonacci implementation



riedel wrote:
Christian Christmann schrieb:
Hi,

I'm looking for a non-recursive implementation of the algorithm to
calculate Fibonacci numbers. Any language is OK (C/C++, pseudo code
prefered).

Any hints?

Thank you,
Chris

/* rexx */
numeric digits 64 /* enough for fib(300) */
parse arg a
say fib(a)

Oh god, REXX. In punishment for that, here is my Bourne Shell
implementation:

#! /bin/sh

fib ()
{
n=$1

if [ "$n" -lt 3 ]
then
echo 1
else
mkdir -p 1/1

(
cd 1/1
n=`expr $n - 2`
while [ $n -gt 0 ]
do
{
( basename `pwd` )
echo "+"
( cd .. ; basename `pwd` )
} | fmt | bc | xargs mkdir

cd *

n=`expr $n - 1`
done

pwd | sed -e 's/.*\///'
)

rm -r 1
fi
}

fib "$@"

Like many shell scripts, it might be a little inefficient in places,
and it might do things the hard way here and there, but it works, so
why mess with it?

- Logan
.