1 ! Copyright (c) 2008 Eric Mertens
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math sequences sequences.private locals hints ;
8 ! sequence helper functions
10 : partial-sums ( seq -- sums )
11 0 [ + ] accumulate swap suffix ; inline
13 : (partial-sum-infimum) ( inf sum elt -- inf sum )
14 + [ min ] keep ; inline
16 : partial-sum-infimum ( seq -- seq )
17 0 0 rot [ (partial-sum-infimum) ] each drop ; inline
19 : generate ( n quot -- seq )
20 [ drop ] prepose map ; inline
22 : map-infimum ( seq quot -- min )
23 [ min ] compose 0 swap reduce ; inline
26 ! triangle generator functions
28 : next ( t -- new-t s )
29 615949 * 797807 + 20 2^ rem dup 19 2^ - ; inline
31 : sums-triangle ( -- seq )
32 0 1000 [ 1+ [ next ] generate partial-sums ] map nip ;
36 :: (euler150) ( m -- n )
37 [let | table [ sums-triangle ] |
41 x z + table nth-unsafe
42 [ y z + 1+ swap nth-unsafe ]
43 [ y swap nth-unsafe ] bi -
44 ] map partial-sum-infimum
49 HINTS: (euler150) fixnum ;