Tweak euler043 to run in constant space, add new each-permutation and reduce-permutations combinators to math.combinatorics

db4
Slava Pestov 2008-11-06 11:07:19 -06:00
parent d2b2cda596
commit 666100a07b
2 changed files with 12 additions and 2 deletions
extra
math/combinatorics
project-euler/043

View File

@ -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 ;

View File

@ -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