Re: Fibonacci implementation



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.

.



Relevant Pages

  • Re: Fibonacci implementation
    ... Christian Christmann wrote: ... I'm looking for a non-recursive implementation of the algorithm to ... Have you tried typing "non-recursive fibonacci" into a search engine? ... I found a page showing how to do it in a C/C++ like language 3rd hit ...
    (comp.programming)
  • Re: OT:Has anyone here drive a Lotus Exige?
    ... >> shit styling, that's even longer than Fiat... ... > "Lotii" isn't the plural of anything (in any language). ...
    (rec.autos.sport.f1)