diff options
Diffstat (limited to 'haskell')
| -rw-r--r-- | haskell/044-pentagonal_numbers.hs | 37 | ||||
| -rw-r--r-- | haskell/wip/026-reciprocal_cycles.hs | 24 | ||||
| -rw-r--r-- | haskell/wip/044-pentagonal_numbers.hs | 19 | ||||
| -rw-r--r-- | haskell/wip/081-path_sum_two_ways.hs | 38 | ||||
| -rw-r--r-- | haskell/wip/205-dice_game.hs | 35 |
5 files changed, 134 insertions, 19 deletions
diff --git a/haskell/044-pentagonal_numbers.hs b/haskell/044-pentagonal_numbers.hs new file mode 100644 index 0000000..db479bc --- /dev/null +++ b/haskell/044-pentagonal_numbers.hs @@ -0,0 +1,37 @@ +-- Pentagon numbers +-- Problem 44 +-- +-- Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. +-- The first ten pentagonal numbers are: +-- +-- 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... +-- +-- It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, +-- 70 − 22 = 48, is not pentagonal. +-- +-- Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference +-- are pentagonal and D = |Pk − Pj| is minimised; what is the value of D? + + +import Data.Maybe(isJust, fromJust) + +main = do + print(fromJust $ last $ takeUntil isJust [diffPentagonal p | p <- pentagonals]) + +pentagonals = [(3 * n ^ 2 - n) `div` 2 | n <- [1..]] + +diffPentagonal :: Int -> Maybe Int +diffPentagonal n + | length testBellow == 0 = Nothing + | otherwise = Just $ head testBellow + where testBellow = [k - (n - k) | k <- takeWhile (< n) pentagonals, + isPentagonal (n - k), isPentagonal (k - (n - k))] + +isPentagonal :: Int -> Bool +isPentagonal x = fromInteger (round n) == n + where n = (sqrt (fromIntegral (24 * x + 1)) + 1) / 6 + +takeUntil :: (a -> Bool) -> [a] -> [a] +takeUntil f (x : xs) + | f x = [x] + | otherwise = x : takeUntil f xs diff --git a/haskell/wip/026-reciprocal_cycles.hs b/haskell/wip/026-reciprocal_cycles.hs new file mode 100644 index 0000000..f60920f --- /dev/null +++ b/haskell/wip/026-reciprocal_cycles.hs @@ -0,0 +1,24 @@ +-- Reciprocal cycles +-- +-- Problem 26 +-- A unit fraction contains 1 in the numerator. The decimal representation of the unit +-- fractions with denominators 2 to 10 are given: +-- +-- 1/2 = 0.5 +-- 1/3 = 0.(3) +-- 1/4 = 0.25 +-- 1/5 = 0.2 +-- 1/6 = 0.1(6) +-- 1/7 = 0.(142857) +-- 1/8 = 0.125 +-- 1/9 = 0.(1) +-- 1/10 = 0.1 +-- Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen +-- that 1/7 has a 6-digit recurring cycle. +-- +-- Find the value of d < 1000 for which 1/d contains the longest recurring cycle in +-- its decimal fraction part. + + +main = do + print () diff --git a/haskell/wip/044-pentagonal_numbers.hs b/haskell/wip/044-pentagonal_numbers.hs deleted file mode 100644 index 966cc13..0000000 --- a/haskell/wip/044-pentagonal_numbers.hs +++ /dev/null @@ -1,19 +0,0 @@ --- Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. --- The first ten pentagonal numbers are: --- --- 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... --- --- It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, --- 70 − 22 = 48, is not pentagonal. --- --- Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference --- are pentagonal and D = |Pk − Pj| is minimised; what is the value of D? - - -main = do - -- print (take 10 pentagonals) - let isPentagonal n = n `elem` takeWhile (<=n) pentagonals - pentagonals = [(3 * n ^ 2 - n) `div` 2 | n <- [1..]] - print (head [j + k| k <- pentagonals, j <- pentagonals, - isPentagonal (j + k)]) --, isPentagonal (abs (j - k))]) - diff --git a/haskell/wip/081-path_sum_two_ways.hs b/haskell/wip/081-path_sum_two_ways.hs new file mode 100644 index 0000000..ec513c4 --- /dev/null +++ b/haskell/wip/081-path_sum_two_ways.hs @@ -0,0 +1,38 @@ +-- Path sum: two ways +-- +-- Problem 81 +-- In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, +-- by only moving to the right and down, is indicated in bold red and is equal to 2427. +-- +-- 131 673 234 103 18 +-- 201 96 342 965 150 +-- 630 803 746 422 111 +-- 537 699 497 121 956 +-- 805 732 524 37 331 +-- +-- Find the minimal path sum, in matrix.txt (right click and "Save Link/Target As..."), +-- a 31K text file containing a 80 by 80 matrix, from the top left to the +-- bottom right by only moving right and down. + +main = do + content <- readFile "../data/081_matrix.txt" + -- print (content) + let rhombus = parseMatrix content + print (rhombus) + + +rectToRhombus :: [[Int]] -> [[Int]] + + +parseMatrix :: String -> [[Int]] +parseMatrix str = map (map (\x -> read x :: Int)) strList + where strList = map (split ',') (lines str) + +-- https://gist.github.com/yamanobori-old/0f74739f2a97d31ebe05 +split :: Eq a => a -> [a] -> [[a]] +split _ [] = [[]] +split delim str = + let (before, remainder) = span (/= delim) str + in before : case remainder of [] -> [] + x -> split delim $ tail x + diff --git a/haskell/wip/205-dice_game.hs b/haskell/wip/205-dice_game.hs new file mode 100644 index 0000000..d21623c --- /dev/null +++ b/haskell/wip/205-dice_game.hs @@ -0,0 +1,35 @@ +-- Dice Game +-- +-- Problem 205 +-- Peter has nine four-sided (pyramidal) dice, each with faces numbered 1, 2, 3, 4. +-- Colin has six six-sided (cubic) dice, each with faces numbered 1, 2, 3, 4, 5, 6. +-- +-- Peter and Colin roll their dice and compare totals: the highest total wins. The +-- result is a draw if the totals are equal. +-- +-- What is the probability that Pyramidal Pete beats Cubic Colin? Give your answer +-- rounded to seven decimal places in the form 0.abcdefg + + +import System.Random +import Data.List + +main = do + gen <- getStdGen + let n = 20000 + peter = map sum $ splitIndex 9 $ randN (1, 4) gen (9 * n) + colin = map sum $ splitIndex 6 $ randN (1, 6) gen (6 * n) + let score = length $ filter (\(p, c) -> p > c) (zip peter colin) + print ((fromIntegral score) / (fromIntegral n)) + +splitIndex :: Int -> [Int] -> [[Int]] +splitIndex n xs + | length xs < n = [] + | otherwise = h : splitIndex n t + where (h, t) = splitAt n xs + +randN :: (Int, Int) -> StdGen -> Int -> [Int] +randN _ _ 0 = [] +randN r gen n = fst next : randN r (snd next) (n - 1) + where next = randomR r gen :: (Int, StdGen) + |
