Paste: Shootout - Pidigts
Author: | sunwukong |
Mode: | factor |
Date: | Sat, 30 May 2009 03:43:24 |
Plain Text |
USING: arrays io kernel make math math.functions math.parser namespaces
sequences ;
IN: shootout-pidigits
CONSTANT: line-length 10
: unit ( -- z ) { 1 0 0 1 } ;
: lifts ( k -- z ) dup 2 * 1+ dup 2 * 0 rot 4array ;
: extr ( z x -- y )
[ 2 cut-slice ] dip
[ over first * swap second + ] curry bi@ / ;
: comp-1 ( z1 z2 i-array -- n ) dup second rot nth -rot first swap nth * ;
: comp ( z1 z2 -- z3 )
{ { 0 0 1 2 } { 0 1 1 3 } { 2 0 3 2 } { 2 1 3 3 } }
[ [ 2dup ] dip 2 cut-slice [ comp-1 ] dip swap [ comp-1 ] dip + ]
with with map ;
: prod ( z n -- z' ) 10 swap -10 * 0 1 4array swap comp ;
: next-safe? ( z -- n/f )
[ 3 extr floor ] [ 4 extr floor ] bi
over = [ drop f ] unless ;
: pi-stream ( z k digits -- )
[ rot dup next-safe? dup
[ dup , prod -rot 1- ]
[ drop rot [ lifts comp ] keep 1+ rot ]
if dup 0 >
] loop 3drop ;
: pidigit-array ( n -- array ) unit 1 rot [ pi-stream ] { } make ;
: print-running-total ( n -- ) "\t:" write number>string print ;
: print-last-line ( n -- )
dup line-length mod dup 0 = [ 2drop ]
[ line-length swap - [ " " write ] times print-running-total ] if ;
: print-pidigits ( n -- )
dup pidigit-array
[ swap number>string write 1+ dup line-length mod 0 =
[ print-running-total ] [ drop ] if
] each-index print-last-line ;
Author: | sunwukong |
Mode: | factor |
Date: | Sat, 30 May 2009 06:09:07 |
Plain Text |
:: extr ( z x -- y )
[let | q [ z first ] r [ z second ] s [ z third ] T [ z fourth ]
x [ x ] |
[infix (q*x+r)/(s*x+T) infix]
] ;
:: comp ( z1 z2 -- z3 )
[let | q [ z1 first ] r [ z1 second ] s [ z1 third ] T [ z1 fourth ]
u [ z2 first ] v [ z2 second ] w [ z2 third ] x [ z2 fourth ] |
[infix q*u+r*w infix] [infix q*v+r*x infix]
[infix s*u+T*w infix] [infix s*v+T*x infix]
] 4array ;
Author: | R` |
Mode: | factor |
Date: | Sat, 30 May 2009 13:19:57 |
Plain Text |
: (n>z) ( n -- z )
[ 10 ] dip -10 * 0 1 4array ;
: prod ( z n -- z' ) (n>z) swap comp ;
: pidigit-array ( n -- array ) [ unit 1 ] dip [ pi-stream ] { } make ;
: lifts ( k -- z )
dup 2 * 1+
[ 2 * 0 ] keep
4array ;
for example:
z1 first4 { [ :> q ] [ :> r ] [ :> s ] [ :> T ] } spread
although the let solution is good also.
note: all of the above code is untested
New Annotation