Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 45 lines (36 sloc) 1.638 kb
eb8bc991 »
2011-02-03 more on 17
1 import Data.Char
2
842b3e73 »
2011-02-02 Started #17
3 onesWords = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
4 tensWords = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
3308e26e »
2011-02-03 more work on 17
5 teenWords = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
6
e7d5bdc4 »
2011-02-04 more on 17
7 tensConnect = ("", "-")
8 hundredsConnect = (" hundred", " and ")
4a7efee4 »
2011-02-04 completed #17
9 thousandsConnect = (" thousand", ", ")
e7d5bdc4 »
2011-02-04 more on 17
10
4a7efee4 »
2011-02-04 completed #17
11 -- Works up to 999,999. Wouldn't take much to extend it much beyond that.
12 sayIt :: Int -> String
13 sayIt n = connect first second thousandsConnect
14 where
15 first = list !! 0
16 second = list !! 1
17 list = thousandsList n
18
19 thousandsList :: Int -> [String]
20 thousandsList = map threeDigitsToWords . commas
21 where
22 commas n = quot n 1000 : [mod n 1000]
23 threeDigitsToWords = tripleToWords . makeTriple
24 makeTriple n = (quot n 100, (mod (quot n 10) 10, mod n 10))
842b3e73 »
2011-02-02 Started #17
25
3308e26e »
2011-02-03 more work on 17
26 doubleToWords :: (Int, Int) -> String
27 doubleToWords (t, o)
28 | t == 1 = teenWords !! o
e7d5bdc4 »
2011-02-04 more on 17
29 | otherwise = connect (tensWords !! t) (onesWords !! o) tensConnect
842b3e73 »
2011-02-02 Started #17
30
3308e26e »
2011-02-03 more work on 17
31 tripleToWords :: (Int, (Int, Int)) -> String
e7d5bdc4 »
2011-02-04 more on 17
32 tripleToWords (h, to) = connect (onesWords !! h) (doubleToWords to) hundredsConnect
33
34 connect :: String -> String -> (String, String) -> String
35 connect "" second connect = second
36 connect first "" connect = first ++ fst connect
37 connect first second connect = first ++ fst connect ++ snd connect ++ second
3308e26e »
2011-02-03 more work on 17
38
eb8bc991 »
2011-02-03 more on 17
39 totalLetters :: [String] -> Int
40 totalLetters words = sum $ map lettersInString words
41 where lettersInString = length . filter isLetter
42
43 euler17 :: Int -> Int
4a7efee4 »
2011-02-04 completed #17
44 euler17 = totalLetters . countTo
45 where countTo n = map sayIt [1..n]
Something went wrong with that request. Please try again.