Difference between revisions of "Euler problems/81 to 90"

From HaskellWiki
Jump to navigation Jump to search
Line 5: Line 5:
 
Solution:
 
Solution:
 
<haskell>
 
<haskell>
  +
import Data.List (unfoldr)
problem_81 = undefined
 
  +
  +
columns s = unfoldr f s
  +
where
  +
f [] = Nothing
  +
f xs = Just $ (\(a,b) -> (read a, drop 1 b)) $ break (==',') xs
  +
  +
firstLine ls = scanl1 (+) ls
  +
  +
nextLine pl [] = pl
  +
nextLine pl (n:nl) = nextLine p' nl
  +
where
  +
p' = nextCell (head pl) pl n
  +
nextCell _ [] [] = []
  +
nextCell pc (p:pl) (n:nl) = pc' : nextCell pc' pl nl
  +
where pc' = n + min p pc
  +
  +
minSum (p:nl) = last $ nextLine p' nl
  +
where
  +
p' = firstLine p
  +
  +
problem_81 c = minSum $ map columns $ lines c
 
</haskell>
 
</haskell>
   

Revision as of 13:20, 24 August 2007

Problem 81

Find the minimal path sum from the top left to the bottom right by moving right and down.

Solution:

import Data.List (unfoldr)

columns s = unfoldr f s
    where
        f [] = Nothing
        f xs = Just $ (\(a,b) -> (read a, drop 1 b)) $ break (==',') xs

firstLine ls = scanl1 (+) ls

nextLine pl [] = pl
nextLine pl (n:nl) = nextLine p' nl
    where
        p' = nextCell (head pl) pl n
        nextCell _ [] [] = []
        nextCell pc (p:pl) (n:nl) = pc' : nextCell pc' pl nl
            where pc' = n + min p pc

minSum (p:nl) = last $ nextLine p' nl
    where
        p' = firstLine p

problem_81 c = minSum $ map columns $ lines c

Problem 82

Find the minimal path sum from the left column to the right column.

Solution:

problem_82 = undefined

Problem 83

Find the minimal path sum from the top left to the bottom right by moving left, right, up, and down.

Solution:

problem_83 = undefined

Problem 84

In the game, Monopoly, find the three most popular squares when using two 4-sided dice.

Solution:

problem_84 = undefined

Problem 85

Investigating the number of rectangles in a rectangular grid.

Solution:

problem_85 = undefined

Problem 86

Exploring the shortest path from one corner of a cuboid to another.

Solution:

problem_86 = undefined

Problem 87

Investigating numbers that can be expressed as the sum of a prime square, cube, and fourth power?

Solution:

import List

problem_87 = length expressible
    where limit = 50000000
          squares = takeWhile (<limit) (map (^2) primes)
          cubes   = takeWhile (<limit) (map (^3) primes)
          fourths = takeWhile (<limit) (map (^4) primes)
          choices = [[s,c,f] | s <- squares, c <- cubes, f <- fourths]
          unique  = map head . group . sort
          expressible = filter (<limit) . unique . map sum $ choices

Problem 88

Exploring minimal product-sum numbers for sets of different sizes.

Solution:

problem_88 = undefined

Problem 89

Develop a method to express Roman numerals in minimal form.

Solution:

problem_89 = undefined

Problem 90

An unexpected way of using two cubes to make a square.

Solution:

problem_90 = undefined