Tweak euler043 to run in constant space, add new each-permutation and reduce-permutations combinators to math.combinatorics
parent
d2b2cda596
commit
666100a07b
|
@ -44,5 +44,12 @@ PRIVATE>
|
|||
: all-permutations ( seq -- seq )
|
||||
[ length factorial ] keep '[ _ permutation ] map ;
|
||||
|
||||
: each-permutation ( seq quot -- )
|
||||
[ [ length factorial ] keep ] dip
|
||||
'[ _ permutation @ ] each ; inline
|
||||
|
||||
: reduce-permutations ( seq initial quot -- result )
|
||||
swapd each-permutation ; inline
|
||||
|
||||
: inverse-permutation ( seq -- permutation )
|
||||
<enum> >alist sort-values keys ;
|
||||
|
|
|
@ -52,8 +52,11 @@ IN: project-euler.043
|
|||
PRIVATE>
|
||||
|
||||
: euler043 ( -- answer )
|
||||
1234567890 number>digits all-permutations
|
||||
[ interesting? ] filter [ 10 digits>integer ] map sum ;
|
||||
1234567890 number>digits 0 [
|
||||
dup interesting? [
|
||||
10 digits>integer +
|
||||
] [ drop ] if
|
||||
] reduce-permutations ;
|
||||
|
||||
! [ euler043 ] time
|
||||
! 104526 ms run / 42735 ms GC time
|
||||
|
|
Loading…
Reference in New Issue