Paste: Project Euler # 1

Author: Loryn
Mode: factor
Date: Sun, 19 Apr 2009 06:13:39
Plain Text |
TUPLE: numeric-range lower upper increment range ;

: next-increment ( numeric-range -- n )
    [ increment>> ] [ range>> ] bi peek + ;

: append ( numeric-range -- numeric-range )
    dup next-increment swap [ range>> ] keep [ push ] dip ;

: continue? ( numeric-range -- ? )
    [ next-increment ] [ upper>> ] bi < ;

: generate ( numeric-range -- numeric-range )
    [ dup continue? ] [ append ] while ;


( scratchpad ) 0 1000 5 V{ 0 } numeric-range boa
( scratchpad ) 0 1000 3 V{ 0 } numeric-range boa
( scratchpad ) [ generate ] bi@ [ range>> ] bi@ union sum

Annotation: Slightly more idiomatic...

Author: Loryn
Mode: factor
Date: Sun, 19 Apr 2009 07:23:39
Plain Text |
TUPLE: numeric-range lower upper increment range ;

: next-increment ( numeric-range -- n )
    [ increment>> ] [ range>> ] bi peek + ;

: append ( numeric-range -- )
    dup next-increment swap [ range>> ] keep [ push ] dip drop ;

: continue? ( numeric-range -- ? )
    [ next-increment ] [ upper>> ] bi < ;

: generate ( numeric-range -- )
    [ dup continue? ] [ dup append ] while drop ;

( scratchpad ) 0 1000 5 V{ 0 } numeric-range boa
( scratchpad ) 0 1000 3 V{ 0 } numeric-range boa
( scratchpad ) [ dup generate ] bi@ [ range>> ] bi@ union sum

Annotation: Much better solutions...

Author: Loryn
Mode: factor
Date: Sun, 19 Apr 2009 08:36:44
Plain Text |
This one, thanks to glguy:

0 999 3 <range> 0 999 5 <range> union sum



And this one, able to take arbitrary number of ranges, thanks to Slava / glguy / hotaru2k3:


{ 3 5 } [ [ 999 ] keep <range> ] gather sum

New Annotation

Summary:
Author:
Mode:
Body: