What is the Largest Value?

Difficulty: ★ ★ ☆ ☆

Of all the crazy stuff that happens in the realm of mathematics, I’m certain that mathematicians must enjoy solving math puzzles — just like I assume that good programmers enjoy solving programming puzzles.

Unlike programmers, the math nerds most likely break out the slide rules, graph paper, protractors, and what-have-you to solve their mysterious math problems. But for C programmers, the computer solves does the work — providing that you write good code. This point is where the two artforms intersect.

For this month’s programming exercise, the math problem reads like this:

Find all positive values a+b+c+d = 36. Then determine which is the largest value generated for ab+bc+cd?

My first reaction upon reading the puzzle was, “Who cares?” Well, obviously math nerds may find some reason why performing this task is important or necessary. But for a programmer, your job is to write the code to reveal the answer.

For this challenge, assume that zero is a positive value. You need not output every solution for a+b+c+d = 36. No, just output the largest value possible for (a*b)+(b*c)+(c*d) whenever a+b+c+d = 36.

My solution consists of only two statements, the second being the printf() statement that outputs the result: 324

Please try this exercise on your own before you peek at my solution, which I’ll post in a week.

Leave a Reply