## Posts Tagged ‘multilplication’

### Programming Praxis – Three Wise Men

December 28, 2012

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.