From 666100a07bc47a9542f45e179eacb8f731cec898 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 6 Nov 2008 11:07:19 -0600 Subject: [PATCH] Tweak euler043 to run in constant space, add new each-permutation and reduce-permutations combinators to math.combinatorics --- extra/math/combinatorics/combinatorics.factor | 7 +++++++ extra/project-euler/043/043.factor | 7 +++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/extra/math/combinatorics/combinatorics.factor b/extra/math/combinatorics/combinatorics.factor index b1c49b8ab5..00a104b381 100644 --- a/extra/math/combinatorics/combinatorics.factor +++ b/extra/math/combinatorics/combinatorics.factor @@ -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 ) >alist sort-values keys ; diff --git a/extra/project-euler/043/043.factor b/extra/project-euler/043/043.factor index 268a6becfb..37118b88a3 100644 --- a/extra/project-euler/043/043.factor +++ b/extra/project-euler/043/043.factor @@ -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