In today’s Programming Praxis exercise, our goal is to solve a puzzle in which both the addition and multiplication of three numbers results in 65.52. Let’s get started, shall we?

Assuming each item costs at least one penny, the most expensive item can be at most 65.50. By multiplying each amount by 100 we can avoid any potential inaccuracies resulting from using floating point numbers. By stating that a must be more expensive than b we remove some duplicates and reduce the search space. Once a and b are known c follows automatically, otherwise the addition would be incorrect. Finally, we check if the multiplication works. Since all three numbers have been multiplied by 100, the result must be 65.52 * 100^3.

main :: IO ()
main = print $ head [(a,b,c) | a <- [1..6550], b <- [1..a]
, let c = 6552 - a - b
, a * b * c == 65520000]

This gives us the amounts $2.00, $0.52 and $63.00. Clearly, two of the wise men are cheapskates.

### Like this:

Like Loading...

*Related*

Tags: addition, bonsai, code, Haskell, kata, men, multilplication, praxis, programming, wise

This entry was posted on December 28, 2012 at 12:39 pm and is filed under Programming Praxis. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

## Leave a Reply