2005-03-28 23:45:13 -05:00
|
|
|
IN: temporary
|
2004-10-27 21:21:31 -04:00
|
|
|
USE: compiler
|
2004-12-10 19:29:07 -05:00
|
|
|
USE: kernel
|
2004-08-23 23:27:55 -04:00
|
|
|
USE: math
|
|
|
|
|
USE: test
|
2005-01-06 19:10:02 -05:00
|
|
|
USE: math-internals
|
2005-05-13 18:27:18 -04:00
|
|
|
USE: namespaces
|
2006-05-02 00:11:59 -04:00
|
|
|
USE: words
|
2005-05-13 18:27:18 -04:00
|
|
|
|
2006-04-24 17:52:03 -04:00
|
|
|
! Five fibonacci implementations, each one slower than the
|
2005-05-13 18:27:18 -04:00
|
|
|
! previous.
|
2005-01-06 19:10:02 -05:00
|
|
|
|
2006-04-24 17:52:03 -04:00
|
|
|
: fast-fixnum-fib ( n -- nth fibonacci number )
|
|
|
|
|
dup 1 fixnum<= [
|
|
|
|
|
drop 1
|
|
|
|
|
] [
|
|
|
|
|
1 fixnum-fast dup fast-fixnum-fib
|
|
|
|
|
swap 1 fixnum-fast fast-fixnum-fib fixnum+fast
|
|
|
|
|
] if ;
|
|
|
|
|
compiled
|
|
|
|
|
|
|
|
|
|
[ 9227465 ] [ 34 fast-fixnum-fib ] unit-test
|
|
|
|
|
|
2005-01-06 19:10:02 -05:00
|
|
|
: fixnum-fib ( n -- nth fibonacci number )
|
|
|
|
|
dup 1 fixnum<= [
|
|
|
|
|
drop 1
|
|
|
|
|
] [
|
|
|
|
|
1 fixnum- dup fixnum-fib swap 1 fixnum- fixnum-fib fixnum+
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ;
|
2005-01-06 19:10:02 -05:00
|
|
|
compiled
|
|
|
|
|
|
|
|
|
|
[ 9227465 ] [ 34 fixnum-fib ] unit-test
|
2004-10-27 21:21:31 -04:00
|
|
|
|
|
|
|
|
: fib ( n -- nth fibonacci number )
|
2006-05-02 00:11:59 -04:00
|
|
|
dup 1 <= [ drop 1 ] [ dup 1 - fib swap 2 - fib + ] if ;
|
|
|
|
|
|
|
|
|
|
\ fib { fixnum } "specializer" set-word-prop
|
|
|
|
|
\ fib compile
|
2004-08-23 23:27:55 -04:00
|
|
|
|
2004-10-17 19:01:16 -04:00
|
|
|
[ 9227465 ] [ 34 fib ] unit-test
|
2005-05-09 02:34:15 -04:00
|
|
|
|
|
|
|
|
TUPLE: box i ;
|
|
|
|
|
|
|
|
|
|
: tuple-fib ( n -- n )
|
|
|
|
|
dup box-i 1 <= [
|
|
|
|
|
drop 1 <box>
|
|
|
|
|
] [
|
2005-09-16 22:47:28 -04:00
|
|
|
box-i 1- <box>
|
2005-05-09 02:34:15 -04:00
|
|
|
dup tuple-fib
|
|
|
|
|
swap
|
2005-09-16 22:47:28 -04:00
|
|
|
box-i 1- <box>
|
2005-05-09 02:34:15 -04:00
|
|
|
tuple-fib
|
|
|
|
|
swap box-i swap box-i + <box>
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ; compiled
|
2005-05-09 02:34:15 -04:00
|
|
|
|
2005-10-29 23:25:38 -04:00
|
|
|
[ T{ box f 9227465 } ] [ T{ box f 34 } tuple-fib ] unit-test
|
2005-05-13 18:27:18 -04:00
|
|
|
|
|
|
|
|
SYMBOL: n
|
|
|
|
|
: namespace-fib ( n -- n )
|
|
|
|
|
[
|
|
|
|
|
n set
|
|
|
|
|
n get 1 <= [
|
|
|
|
|
1
|
|
|
|
|
] [
|
|
|
|
|
n get 1 - namespace-fib
|
|
|
|
|
n get 2 - namespace-fib
|
|
|
|
|
+
|
2005-09-24 15:21:17 -04:00
|
|
|
] if
|
2005-05-13 18:27:18 -04:00
|
|
|
] with-scope ; compiled
|
|
|
|
|
|
2006-04-23 02:24:14 -04:00
|
|
|
[ 1346269 ] [ 30 namespace-fib ] unit-test
|