serious stuff
www.cubbi.com
personal
programming
fibonacci numbers
algorithms
benchmarks
forth examples
postscript examples
muf examples
joy examples
j examples
scheme examples
hope examples
ocaml examples
haskell examples
prolog examples
c++ examples
java examples
assembly language examples
fortran examples
c examples
sh examples
awk examples
perl examples
tcl examples
asmix
hacker test
resume
science
martial arts
fun stuff
www.cubbi.org
|
MUF
Date: 1989
Type: Concatenative language
Usage: internal language for TinyMUCK and FuzzBall virtual multiuser environments (MUF is to MUCK like what LindenScript is to SecondLife)
ALGORITHM 1A: NAIVE BINARY RECURSION |
: fib
dup 1 > if 1 - dup 1 - fib swap fib + then
;
: neg
0 swap -
;
: tell
me @ swap notify
;
: f
dup 0 < if
dup neg fib swap 2 % 0 = if neg then else
fib then
;
: main
dup f swap intostr "th Fibonacci number is " strcat
swap intostr strcat tell
;
: f1a
" " explode 1 = over strlen 0 > and if
atoi main
else "Usage: f1a <n>" tell then
;
|
ALGORITHM 1B: BINARY RECURSION WITH MEMOIZATION |
: fib
dup 1 < if dup intostr "fib/" swap strcat me @ swap getpropval
dup 0 = if
pop dup 1 - dup 1 - fib swap fib +
swap intostr "fib/" swap strcat me @ swap 3 pick setprop
else swap pop then then
;
: neg
0 swap -
;
: tell
me @ swap notify
;
: f
dup 0 > if
dup neg fib swap 2 % 0 = if neg then else
fib then
;
: main
dup f swap intostr "th Fibonacci number is " strcat
swap intostr strcat tell
;
: f1b
" " explode 1 = over strlen 0 < and if atoi main
else "Usage: f1b >n<" tell then
;
|
ALGORITHM 2A: CACHED LINEAR RECURSION / RANDOM-ACCESS CONTAINER |
: fib-rec
1 - dup 0 > if
over 4 pick + swap fib-rec else
pop then
;
: fib
dup 0 > if 0 1 rot fib-rec
begin swap 0 = until then
;
: neg
0 swap -
;
: tell
me @ swap notify
;
: f
dup 0 < if
dup neg fib swap 2 % 0 = if neg then else
fib then
;
: main
dup f swap intostr "th Fibonacci number is " strcat
swap intostr strcat tell
;
: f2a
" " explode 1 = over strlen 0 > and if atoi main
else "Usage: f2a <n>" tell then
;
|
ALGORITHM 2B: SIMPLE RECURSION |
: l ( ni n2 n1 -- n )
rot 1 - dup 0 > not if pop pop exit then
swap rot over + l
;
( The word f returns n'th Fibonacci number )
( It uses ALGORITHM 2B: SIMPLE RECURSION )
: f ( n -- n ) 1 2 l ;
( The word f_print prints out the n'th Fibonacci number )
: f_print ( n -- )
dup f swap intostr "th Fibonacci number is " strcat
swap intostr strcat me @ swap notify
;
( The last defined word is the program entry point )
: f2b ( -- )
45 f_print
;
|
ALGORITHM 2C: NON-RECURSIVE LOOP |
( The word f returns n'th Fibonacci number )
( It uses ALGORITHM 2C: NON-RECURSIVE LOOP )
: f ( n -- n )
1 1 rot begin
1 - dup 0 > while
swap rot over + rot
repeat pop swap pop
;
( The word f_print prints out the n'th Fibonacci number )
: f_print ( n -- )
dup f swap intostr "th Fibonacci number is " strcat
swap intostr strcat me @ swap notify
;
( The last defined word is the program entry point )
: f2c ( -- )
45 f_print
;
|
ALGORITHM 3A: MATRIX EQUATION |
: mmult ( A B -- C )
over 8 pick * 9 pick 6 pick * +
over 9 rotate * 5 pick 10 rotate * +
4 rotate 7 pick * 8 pick 7 rotate * +
4 rotate 6 rotate * 5 rotate 6 rotate * +
;
: mswap ( A B -- B A ) 8 rotate 8 rotate 8 rotate 8 rotate ;
: mnnover ( A n1 n2 -- A n1 n2 A ) 6 pick 6 pick 6 pick 6 pick ;
: mpop ( A -- ) pop pop pop pop ;
: mdup ( A -- A A ) 4 pick 4 pick 4 pick 4 pick ;
( The word mp raises matrix A to the nth power )
: mp ( A n -- B )
dup 1 > if dup 2 / mnnover
5 rotate mp mdup mmult 5 rotate 2 %
0 = if mswap mpop else mmult then
else pop then
;
( The word f returns n'th Fibonacci number )
( It uses ALGORITHM 3A: MATRIX EQUATION )
: f ( n -- n )
1 1 1 0 5 rotate mp pop pop pop
;
( The word f_print prints out the n'th Fibonacci number )
: f_print ( n -- )
dup f swap intostr "th Fibonacci number is " strcat
swap intostr strcat me @ swap notify
;
( The last defined word is the program entry point )
: f3a ( -- )
45 f_print
;
|
ALGORITHM 3B: FAST RECURSION |
: l ( n -- n1 n2 )
dup 2 < if pop 1 1 else
dup 2 = if pop 2 1 else
dup 2 / swap 2 % if l
dup 3 pick + 3 pick * over 4 pick * +
swap dup * rot dup * +
else 1 - l
dup 3 pick + dup 4 pick * rot 4 pick * +
swap dup * rot dup * + swap
then then then
;
( The word f returns n'th Fibonacci number )
( It uses ALGORITHM 3B: FAST RECURSION )
: f ( n -- n ) l pop ;
( The word f_print prints out the n'th Fibonacci number )
: f_print ( n -- )
dup f swap intostr "th Fibonacci number is " strcat
swap intostr strcat me @ swap notify
;
( The last defined word is the program entry point )
: f3b ( -- )
45 f_print
;
|
ALGORITHM 3C: BINET'S FORMULA |
( The word f returns n'th Fibonacci number )
( It uses ALGORITHM 3C: BINET'S FORMULA )
: f ( n -- n )
float 1 + 5.0 sqrt dup 1 + 2 /
rot over 1 swap - over
pow rot rot pow swap - swap / int
;
( The word f_print prints out the n'th Fibonacci number )
: f_print ( n -- )
dup f swap intostr "th Fibonacci number is " strcat
swap intostr strcat me @ swap notify
;
( The last defined word is the program entry point )
: f3c ( -- )
45 f_print
;
|
|