58 lines
1.3 KiB
Forth
58 lines
1.3 KiB
Forth
|
\ compute the nth fibonacci number, starting at 0
|
||
|
: fib ( n -- n2 )
|
||
|
dup ( n n )
|
||
|
2 < if
|
||
|
drop 1 ( 1 )
|
||
|
else
|
||
|
dup 2 - recurse ( n fib[n-2] )
|
||
|
swap ( fib[n-2] n )
|
||
|
1 - recurse ( fib[n-2] fib[n-1] )
|
||
|
+ ( n2 )
|
||
|
then ;
|
||
|
|
||
|
create memo 100 cells allot
|
||
|
|
||
|
\ initialize it
|
||
|
: init ( n -- )
|
||
|
dup ( n n )
|
||
|
if ( n )
|
||
|
dup 0 swap ( n 0 n )
|
||
|
1 - ( n 0 n-1 )
|
||
|
cells memo + ( n n-1+memo )
|
||
|
1 - ( n 0 n-1+memo)
|
||
|
! ( n )
|
||
|
1 - ( n-1 )
|
||
|
recurse
|
||
|
else
|
||
|
drop
|
||
|
then ;
|
||
|
|
||
|
: fib2x ( n -- n2 )
|
||
|
dup ( n n )
|
||
|
2 < if ( n )
|
||
|
drop 1 ( 1 )
|
||
|
else
|
||
|
dup cells memo + ( n n+memo )
|
||
|
@ ( n ${n+memo} )
|
||
|
dup 0 > if ( n ${n+memo} )
|
||
|
swap drop ( ${n+memo} )
|
||
|
else
|
||
|
drop ( n )
|
||
|
dup dup ( n n n )
|
||
|
2 - recurse ( n n fib2[n-2] )
|
||
|
swap ( n fib2[n-2] n )
|
||
|
1 - recurse ( n fib2[n-2] fib2[n-1] )
|
||
|
+ ( n fib2[n] )
|
||
|
dup rot ( fib2[n] fib2[n] n )
|
||
|
cells memo + ! ( fib2[n] )
|
||
|
then
|
||
|
then ;
|
||
|
|
||
|
: fib2
|
||
|
\ allocate a lookup table and run fib2x
|
||
|
100 init fib2x ;
|
||
|
|
||
|
45 fib2
|
||
|
.s cr
|
||
|
bye
|