Merge branch 'master' of http://factorforge.org/glguy.factor
commit
6746bd3d53
|
@ -0,0 +1,55 @@
|
|||
! Copyright (c) 2008 Eric Mertens
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel math math.ranges sequences sequences.lib ;
|
||||
|
||||
IN: project-euler.116
|
||||
|
||||
! http://projecteuler.net/index.php?section=problems&id=116
|
||||
|
||||
! DESCRIPTION
|
||||
! -----------
|
||||
|
||||
! A row of five black square tiles is to have a number of its tiles replaced
|
||||
! with coloured oblong tiles chosen from red (length two), green (length
|
||||
! three), or blue (length four).
|
||||
|
||||
! If red tiles are chosen there are exactly seven ways this can be done.
|
||||
! If green tiles are chosen there are three ways.
|
||||
! And if blue tiles are chosen there are two ways.
|
||||
|
||||
! Assuming that colours cannot be mixed there are 7 + 3 + 2 = 12 ways of
|
||||
! replacing the black tiles in a row measuring five units in length.
|
||||
|
||||
! How many different ways can the black tiles in a row measuring fifty units in
|
||||
! length be replaced if colours cannot be mixed and at least one coloured tile
|
||||
! must be used?
|
||||
|
||||
! SOLUTION
|
||||
! --------
|
||||
|
||||
! This solution uses a simple dynamic programming approach using the
|
||||
! following recurence relation
|
||||
|
||||
! ways(n,_) = 0 | n < 0
|
||||
! ways(0,_) = 1
|
||||
! ways(n,i) = ways(n-i,i) + ways(n-1,i)
|
||||
! solution(n) = ways(n,1) - 1 + ways(n,2) - 1 + ways(n,3) - 1
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: nth* ( n seq -- elt/0 )
|
||||
[ length swap - 1- ] keep ?nth 0 or ;
|
||||
|
||||
: next ( colortile seq -- )
|
||||
[ nth* ] [ peek + ] [ push ] tri ;
|
||||
|
||||
: ways ( length colortile -- permutations )
|
||||
V{ 1 } clone [ [ next ] 2curry times ] keep peek 1- ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: (euler116) ( length -- permutations )
|
||||
3 [1,b] [ ways ] with sigma ;
|
||||
|
||||
: euler116 ( -- permutations )
|
||||
50 (euler116) ;
|
|
@ -0,0 +1,42 @@
|
|||
! Copyright (c) 2008 Eric Mertens
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel math splitting sequences ;
|
||||
|
||||
IN: project-euler.117
|
||||
|
||||
! http://projecteuler.net/index.php?section=problems&id=117
|
||||
|
||||
! DESCRIPTION
|
||||
! -----------
|
||||
|
||||
! Using a combination of black square tiles and oblong tiles chosen
|
||||
! from: red tiles measuring two units, green tiles measuring three
|
||||
! units, and blue tiles measuring four units, it is possible to tile a
|
||||
! row measuring five units in length in exactly fifteen different ways.
|
||||
|
||||
! How many ways can a row measuring fifty units in length be tiled?
|
||||
|
||||
! SOLUTION
|
||||
! --------
|
||||
|
||||
! This solution uses a simple dynamic programming approach using the
|
||||
! following recurence relation
|
||||
|
||||
! ways(i) = 1 | i <= 0
|
||||
! ways(i) = ways(i-4) + ways(i-3) + ways(i-2) + ways(i-1)
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: short ( seq n -- seq n )
|
||||
over length min ;
|
||||
|
||||
: next ( seq -- )
|
||||
[ 4 short tail* sum ] keep push ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: (euler117) ( n -- m )
|
||||
V{ 1 } clone tuck [ next ] curry times peek ;
|
||||
|
||||
: euler117 ( -- m )
|
||||
50 (euler117) ;
|
|
@ -0,0 +1,44 @@
|
|||
! Copyright (c) 2008 Eric Mertens
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel math sequences locals ;
|
||||
IN: project-euler.150
|
||||
|
||||
<PRIVATE
|
||||
|
||||
! sequence helper functions
|
||||
|
||||
: partial-sums ( seq -- seq )
|
||||
0 [ + ] accumulate swap suffix ; inline
|
||||
|
||||
: generate ( n quot -- seq )
|
||||
[ drop ] swap compose map ; inline
|
||||
|
||||
: map-infimum ( seq quot -- min )
|
||||
[ min ] compose 0 swap reduce ; inline
|
||||
|
||||
|
||||
! triangle generator functions
|
||||
|
||||
: next ( t -- new-t s )
|
||||
615949 * 797807 + 1 20 shift mod dup 1 19 shift - ; inline
|
||||
|
||||
: sums-triangle ( -- seq )
|
||||
0 1000 [ 1+ [ next ] generate partial-sums ] map nip ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
:: (euler150) ( m -- n )
|
||||
[let | table [ sums-triangle ] |
|
||||
m [| x |
|
||||
x 1+ [| y |
|
||||
m x - [| z |
|
||||
x z + table nth
|
||||
[ y z + 1+ swap nth ]
|
||||
[ y swap nth ] bi -
|
||||
] map partial-sums infimum
|
||||
] map-infimum
|
||||
] map-infimum
|
||||
] ;
|
||||
|
||||
: euler150 ( -- n )
|
||||
1000 (euler150) ;
|
Loading…
Reference in New Issue