Programming Praxis – Three Wise Men

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.

About these ads

Tags: , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: