serious stuff
www.cubbi.com
personal
programming
fibonacci numbers
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

J
Date: 1990
Type: Vector language
Usage: statistics, science, and everything else APL does. (J is APL's successor)
ALGORITHM 1A: NAIVE BINARY RECURSION 
fib =: (&2 +&$: <:) ^: (1&<)
f =: fib&**^>:
n =: _".>{:ARGV
main =: (":,'th Fibonacci number is '"_,":&f) :: ('Usage: jconsole f1a.ijs <n>'"_)
echo main n
exit''

ALGORITHM 1B: CACHED BINARY RECURSION 
fib =: (&2 +&$: <:) ^: (1&<) M.
f =: (fib&**^>:)&x:
n =: _".>{:ARGV
main =: (":,'th Fibonacci number is '"_,":&f) :: ('Usage: jconsole f1b.ijs <n>'"_)
echo main n
exit''

ALGORITHM 2A: CACHED LINEAR RECURSION / RANDOMACCESS CONTAINER. 
flist =: 0 1x"_` ((], +/&(_2&{.))&$:&<:) @. (1&<)
fib=: {:&flist
f =: (fib&**^>:)&x:
n =: _".>{:ARGV
main =: (":,'th Fibonacci number is '"_,":&f) :: ('Usage: jconsole f2a.ijs <n>'"_)
echo main n
exit''

ALGORITHM 2B: LINEAR RECURSION WITH ACCUMULATOR 
fib=: {:@($:&1x 0) : ((<:@[ $: +/\@.@])^:(*@[))
f =: (fib&**^>:)&x:
n =: _".>{:ARGV
main =: (":,'th Fibonacci number is '"_,":&f) :: ('Usage: jconsole f2b.ijs <n>'"_)
echo main n
exit''

ALGORITHM 2C: IMPERATIVE LOOP WITH MUTABLE VARIABLES 
fib =: {:&([+/\@.@]^:[1x 0"0)
f =: (fib&**^>:)&x:
n =: _".>{:ARGV
main =: (":,'th Fibonacci number is '"_,":&f) :: ('Usage: jconsole f2c.ijs <n>'"_)
echo main n
exit''

ALGORITHM 3A: MATRIX MULTIPLICATION 
mm =: +/ .*
mp =: 4 : 'mm / ( mm ~^: (I..#: y ) x)'
fib =: {.&,&((2 2$1x 1 1 0)&mp&<:)
f =: (fib&**^>:)&x:
n =: _".>{:ARGV
main =: (":,'th Fibonacci number is '"_,":&f) :: ('Usage: jconsole f3a.ijs <n>'"_)
echo main n
exit''

ALGORITHM 3B: FAST RECURSION 
fib=: 3 : 0
if. 2 >: y do. 1<.y else.
if. y = 2*n=.<.y%2
do. (n+1) &*:&fib n1
else. (n+1) +&*:&fib n end.
end.
)
f =: (fib&**^>:)&x:
n =: _".>{:ARGV
main =: (":,'th Fibonacci number is '"_,":&f) :: ('Usage: jconsole f3b.ijs >n<'"_)
echo main n
exit''

ALGORITHM 3C: BINET'S FORMULA 
times=: (1 5&(+/ .*)@:* , (+/ .* .)) " 1
pow =: 4 : 'times/ 1 0 , times~^:(I..#:y) x' " 1 0
fib =: {:@(1 1x&pow) % 2x&^@<:
f =: (fib&**^>:)&x:
n =: _".>{:ARGV
main =: (":,'th Fibonacci number is '"_,":&f) :: ('Usage: jconsole f3c.ijs <n>'"_)
echo main n
exit''

