Re: Fibonacci implementation
- From: Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx>
- Date: Tue, 29 Aug 2006 02:48:56 GMT
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
.
- Follow-Ups:
- Re: Fibonacci implementation
- From: Rob Thorpe
- Re: Fibonacci implementation
- References:
- Fibonacci implementation
- From: Christian Christmann
- Re: Fibonacci implementation
- From: riedel
- Fibonacci implementation
- Prev by Date: Re: Code Comprehension
- Next by Date: Re: Code Comprehension
- Previous by thread: Re: Fibonacci implementation
- Next by thread: Re: Fibonacci implementation
- Index(es):