]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/117/117.factor
factor: trim using lists
[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 project-euler.common sequences ;
4 IN: project-euler.117
5
6 ! http://projecteuler.net/index.php?section=problems&id=117
7
8 ! DESCRIPTION
9 ! -----------
10
11 ! Using a combination of black square tiles and oblong tiles chosen
12 ! from: red tiles measuring two units, green tiles measuring three
13 ! units, and blue tiles measuring four units, it is possible to tile a
14 ! row measuring five units in length in exactly fifteen different ways.
15
16 ! How many ways can a row measuring fifty units in length be tiled?
17
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 : next ( seq -- )
31     [ 4 short tail* sum ] keep push ;
32
33 : (euler117) ( n -- m )
34     [ V{ 1 } clone ] dip over [ next ] curry times last ;
35
36 PRIVATE>
37
38 : euler117 ( -- answer )
39     50 (euler117) ;
40
41 ! [ euler117 ] 100 ave-time
42 ! 0 ms ave run time - 0.29 SD (100 trials)
43
44 SOLUTION: euler117