]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/089/089.factor
project-euler: Rewrap, update links, add copyrights, tests
[factor.git] / extra / project-euler / 089 / 089.factor
1 ! Copyright (c) 2009 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: io.encodings.ascii io.files kernel math
4 project-euler.common roman sequences ;
5 IN: project-euler.089
6
7 ! https://projecteuler.net/problem=89
8
9 ! DESCRIPTION
10 ! -----------
11
12 ! The rules for writing Roman numerals allow for many ways of
13 ! writing each number (see FAQ: Roman Numerals). However, there
14 ! is always a "best" way of writing a particular number.
15
16 ! For example, the following represent all of the legitimate
17 ! ways of writing the number sixteen:
18
19 ! IIIIIIIIIIIIIIII
20 ! VIIIIIIIIIII
21 ! VVIIIIII
22 ! XIIIIII
23 ! VVVI
24 ! XVI
25
26 ! The last example being considered the most efficient, as it
27 ! uses the least number of numerals.
28
29 ! The 11K text file, roman.txt (right click and 'Save
30 ! Link/Target As...'), contains one thousand numbers written in
31 ! valid, but not necessarily minimal, Roman numerals; that is,
32 ! they are arranged in descending units and obey the subtractive
33 ! pair rule (see FAQ for the definitive rules for this problem).
34
35 ! Find the number of characters saved by writing each of these
36 ! in their minimal form.
37
38 ! SOLUTION
39 ! --------
40
41 : euler089 ( -- n )
42     "resource:extra/project-euler/089/roman.txt" ascii file-lines
43     [ ] [ [ roman> >roman ] map ] bi
44     [ [ length ] map-sum ] bi@ - ;
45
46 ! [ euler089 ] 100 ave-time
47 ! 14 ms ave run time - 0.27 SD (100 trials)
48
49 SOLUTION: euler089