1 ! Copyright (c) 2008 Eric Mertens.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs kernel locals math math.order math.ranges sequences ;
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?
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
46 : (euler076) ( n -- m )
48 [ [ ways ] curry each-subproblem ]
49 [ [ dup 2array ] dip at 1- ] 2bi ;
53 : euler076 ( -- answer )
56 ! [ euler076 ] 100 ave-time
57 ! 560 ms ave run time - 17.74 SD (100 trials)