Name: Anonymous 2007-09-14 9:02 ID:w+iUxD20
: n! ( n -- n! ) dup zero? [ drop 1 ] [ dup 1- n! * ] if ;
: ln! ( n lim -- ) over - [ dup n! . 1+ ] times drop ;
1 10 ln!
1
2
6
24
120
720
5040
40320
362880Superior.
: n! ( n -- n! ) dup zero? [ drop 1 ] [ dup 1- n! * ] if ;
: ln! ( n lim -- ) over - [ dup n! . 1+ ] times drop ;
1 10 ln!
1
2
6
24
120
720
5040
40320
362880
: knuth ( x y -- z ) swap <array> 1 [ swap ^ ] reduce ;
: n$ ( n -- n$ ) n! dup knuth ;"memoize" require
USING: memoize math.vectors ;
MEMO: memo-* ( a b -- c ) * ;
MEMO: n! ( n -- n! ) 1+ 1 tail 1 [ memo-* ] reduce ;
: ln! ( n lim -- ) swap tail [ n! . ] each ;( scratchpad ) : n! ( n -- n! ) dup zero? [ drop 1 ] [ dup 1- n! * ] if ;
( scratchpad ) : ln! ( n lim -- ) over - [ dup n! . 1+ ] times drop ;
( scratchpad ) \ n! compile
Compiling n!
( scratchpad ) [ 1 500 ln! ] time
...
3580 ms run / 59 ms GC time
( scratchpad ) "memoize" require
Loading P" resource:extra/memoize/memoize.factor"
Compiling 980 words...
Compile finished.
:errors - print 0 compiler errors.
:warnings - print 866 compiler warnings.
Loading P" resource:extra/memoize/memoize-docs.factor"
( scratchpad ) USING: memoize math.vectors ;
( scratchpad ) MEMO: memo-* ( a b -- c ) * ;
( scratchpad ) MEMO: n! ( n -- n! ) 1+ 1 tail 1 [ memo-* ] reduce ;
( scratchpad ) : ln! ( n lim -- ) swap tail [ n! . ] each ;
( scratchpad ) \ n! compile
Compiling n!
Compiling memo-*
( scratchpad ) [ 1 500 ln! ] time
3308 ms run / 55 ms GC time[def] mult
[ls]a b[/ls]
[if] [equal][var]a[/var] 1[/equal]
[var]b[/var]
[add][var]b[/var] [mult][sub][var]a[/var] 1[/sub] [var]b[/var][/mult][/add]
[/if]
[/def]
[def] fact
[ls]n[/ls]
[if] [equal][var]n[/var] 0[/equal]
1
[mult][var]n[/var] [fact][sub][var]n[/var] 1[/sub][/fact][/mult]
[/if]
[/def]
[fact]6[/fact]
n lim --
var = n!(n)
while(n++ < lim)
output(var)
var = *(var n)[define [fact n]
[define [fact-iter a n]
[if [= n 1]
a
[fact-iter [* a n] [- n 1]]]]
[fact-iter 1 n]]