Re: Fibonacci implementation
- From: "Rob Thorpe" <robert.thorpe@xxxxxxxxxxxx>
- Date: 29 Aug 2006 03:25:15 -0700
Logan Shaw wrote:
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?
Exactly, it's beautiful, the use of xargs and mkdir inparticular.
Here is the same in Unlambda:-
```s``s``sii`ki
`k.*``s``s`ks
``s`k`s`ks``s``s`ks``s`k`s`kr``s`k`sikk
`k``s`ksk
Also, in Cathol
INCIPIT CANTUS fibonacci CUM fibn
OBSCURA a b c
FIAT a 1
FIAT b 1
NUMERABIS n AB INITIO 3 USQUE AD fibn
FIAT c a+b
FIAT a b
FIAT b c
SI fibn MAIOR 3
SCRIBE c
FRANGE
ALIAS
ORA "Domine te laudamus"
NISI
PROCEDE
ILLUMINANDO REDDE c
AMEN
PAENITENTIAM AGE
FIAT fibo30 @fibonacci 30
EGO TE ABSOLVO
These examples are from the websites of vendors of those languages.
.
- References:
- Fibonacci implementation
- From: Christian Christmann
- Re: Fibonacci implementation
- From: riedel
- Re: Fibonacci implementation
- From: Logan Shaw
- Fibonacci implementation
- Prev by Date: Re: Code Comprehension
- Next by Date: Ramdisk for Win2000 -- Any Vendor Recommendations?
- Previous by thread: Re: Fibonacci implementation
- Next by thread: Re: Fibonacci implementation
- Index(es):
Relevant Pages
|