1 ! Copyright (c) 2008 Eric Mertens
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.order sequences sequences.private
9 ! sequence helper functions
11 : partial-sums ( seq -- sums )
12 0 [ + ] accumulate swap suffix ; inline
14 : (partial-sum-infimum) ( inf sum elt -- inf sum )
15 + [ min ] keep ; inline
17 : partial-sum-infimum ( seq -- seq )
18 0 0 rot [ (partial-sum-infimum) ] each drop ; inline
20 : generate ( n quot -- seq )
21 [ drop ] prepose map ; inline
23 : map-infimum ( seq quot -- min )
24 [ min ] compose 0 swap reduce ; inline
27 ! triangle generator functions
29 : next ( t -- new-t s )
30 615949 * 797807 + 20 2^ rem dup 19 2^ - ; inline
32 : sums-triangle ( -- seq )
33 0 1000 [ 1+ [ next ] generate partial-sums ] map nip ;
37 :: (euler150) ( m -- n )
38 [let | table [ sums-triangle ] |
42 x z + table nth-unsafe
43 [ y z + 1+ swap nth-unsafe ]
44 [ y swap nth-unsafe ] bi -
45 ] map partial-sum-infimum
50 HINTS: (euler150) fixnum ;