Skip to content

Instantly share code, notes, and snippets.

@esehara
Created July 3, 2012 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esehara/3041140 to your computer and use it in GitHub Desktop.
Save esehara/3041140 to your computer and use it in GitHub Desktop.
Project Eular 12 by Haskell
number_spell_count :: Integer -> [Integer]
number_spell_count x
| x == 1 = [1,3]
| x == 2 = [2,3]
| x == 3 = [3,5]
| x == 4 = [4,4]
| x == 5 = [5,4]
| x == 6 = [6,3]
| x == 7 = [7,5]
| x == 8 = [8,5]
| x == 9 = [9,4]
| x == 10 = [10,3]
| x == 11 = [11,6]
| x == 12 = [12,6]
| x == 13 = [13,8]
| x == 14 = [14,8]
| x == 15 = [15,7]
| x == 16 = [16,7]
| x == 17 = [17,9]
| x == 18 = [18,8]
| x == 19 = [19,8]
| x == 1000 = [1000,11]
| x >= 100 && x < 1000 = counter_handred x
| x > 19 && x < 100 = counter_ten x
| otherwise = [0,0]
counter_ten :: Integer -> [Integer]
counter_ten x
| 100 > x && x >= 90 = [90,6]
| 90 > x && x >= 80 = [80,6]
| 80 > x && x >= 70 = [70,7]
| 70 > x && x >= 60 = [60,5]
| 60 > x && x >= 50 = [50,5]
| 50 > x && x >= 40 = [40,5]
| 40 > x && x >= 30 = [30,6]
| 30 > x && x >= 20 = [20,6]
| otherwise = [0,0]
counter_handred :: Integer -> [Integer]
counter_handred x = [x_quot * 100,((number_spell_count x_quot) !! 1) + is_and + 7]
where
x_quot = x `quot` 100
is_and
| x `mod` 100 > 0 = 3
| otherwise = 0
number_wrapper :: Integer -> [Integer]
number_wrapper x = [x,0]
counter :: [Integer] -> [Integer]
counter [x,y]
| x > 0 = counter [x - (number_count !! 0),y + (number_count !! 1)]
| otherwise = [x,y]
where
number_count = number_spell_count x
start_data :: [[Integer]]
start_data = map number_wrapper number_range
number_range :: [Integer]
number_range = [1..1000]
get_number :: [Integer] -> Integer
get_number x = x !! 1
get_numbers :: [[Integer]] -> [Integer]
get_numbers x = map get_number x
main = putStrLn $ show $ sum $ get_numbers $ map counter start_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment