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.

2 thoughts on “What is the Largest Value?

  1. You could draw a rectangle with sides of length a & b, then draw another rectangle on side b with the other side c, then a third rectangle on c with the other side length d. Solving this problem would then give the maximum possible area for a given total length, eg 36.

    That’s a possible geometric representation but there might be a more general algebraic representation which I can’t think of. Questions like “find the largest” (or smallest) are usually optimisation problems with widespread uses.

    I don’t know where you found this but if the person who published or posted it did so without any background or explanation of possible uses then that’s pretty stupid, the sort of thing that gives mathematics a bad name.

  2. Chris, you’re explanation is brilliant! It presents the problem in a practical manner, something I didn’t think of (being more of a mathematical idiot). And, yes, the problem was presented without reference to geometry.

    Thank you!

Leave a Reply