1 ! Copyright (c) 2008 Eric Mertens
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs combinators kernel math sequences
4 math.order math.ranges locals ;
7 ! http://projecteuler.net/index.php?section=problems&id=76
12 ! How many different ways can one hundred be written as a
13 ! sum of at least two positive integers?
18 ! This solution uses dynamic programming and the following
23 ! ways(n,i) = ways(n-i,i) + ways(n,i-1)
28 [1,b] [ 0 2array 0 ] H{ } map>assoc
29 1 { 0 0 } pick set-at ;
32 [ - dup ] keep min ; inline
34 : ways ( n i table -- )
38 [ [ 1- 2array ] dip at ]
39 [ [ use 2array ] dip at + ]
40 [ [ 2array ] dip set-at ] 3tri
43 :: each-subproblem ( n quot -- )
44 n [1,b] [ dup [1,b] quot with each ] each ; inline
48 : (euler076) ( n -- m )
50 [ [ ways ] curry each-subproblem ]
51 [ [ dup 2array ] dip at 1- ] 2bi ;