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