]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/117/117.factor
Merge branch 'master' of git://factorcode.org/git/factor into cairo
[factor.git] / extra / project-euler / 117 / 117.factor
1 ! Copyright (c) 2008 Eric Mertens
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.order splitting sequences ;
4
5 IN: project-euler.117
6
7 ! http://projecteuler.net/index.php?section=problems&id=117
8
9 ! DESCRIPTION
10 ! -----------
11
12 ! Using a combination of black square tiles and oblong tiles chosen
13 ! from: red tiles measuring two units, green tiles measuring three
14 ! units, and blue tiles measuring four units, it is possible to tile a
15 ! row measuring five units in length in exactly fifteen different ways.
16
17 !  How many ways can a row measuring fifty units in length be tiled?
18
19 ! SOLUTION
20 ! --------
21
22 ! This solution uses a simple dynamic programming approach using the
23 ! following recurence relation
24
25 ! ways(i) = 1 | i <= 0
26 ! ways(i) = ways(i-4) + ways(i-3) + ways(i-2) + ways(i-1)
27
28 <PRIVATE
29
30 : short ( seq n -- seq n )
31     over length min ;
32
33 : next ( seq -- )
34     [ 4 short tail* sum ] keep push ;
35
36 PRIVATE>
37
38 : (euler117) ( n -- m )
39     V{ 1 } clone tuck [ next ] curry times peek ;
40
41 : euler117 ( -- m )
42     50 (euler117) ;