The Order of Operations and Precedence, Explained in a Brainteaser
In my personal Facebook feed a few days ago, I saw this gem:
If you can’t see the image, the brainteaser asks the following question: ‘What’s 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 – 1 + 1 + 1 + 1 + 1 + 1 + 1 x 0 equal to?’, and provide four multiple choice options: 0, 14, 16, or 17 (thanks to the folks at FunCage.com for this blog topic fodder).
A quick look at this might yield ‘0’ as an answer (‘anything times zero is zero, Alan!’ I can hear, in chorus). But look again – that’s not what’s really being asked. See, you need to harken back to your Algebra I days and recall your Order of Operations. Or, just remember the line you used to utter when family members embarrassed you: “Please Excuse My Dear Aunt Sally.”
You see, with the 1 x 0 at the end, that part gets evaluated first. That nets out to zero (of course). The remainder of the expression is the just added together (minus that one slick “-1” in the middle) to net out to 14. Let’s take a look at how Mathcad evaluates this.
No surprise here, since a professional engineering calculation software package should, indeed, generate this result. Try evaluating that in a spreadsheet program and it gets tricky. Not impossible, but tricky.
With a little expression manipulation in Mathcad Prime, we can explore it a little, and help us (or a confused friend who answered ‘0’) understand a bit more.
When examined this way, I think most would say, “well of course the answer is 14.” And that’s the lesson here: the order of operations indeed matters when evaluating complex expressions in your product design, or answering silly brainteasers on Facebook. Just think of the bar bets you could win with your less-than-math-savvy friends.
A Few Words on Implicit Multiplication
This one is a bit trickier. I saw this on a reddit Math thread the other day.
If you can’t see the image, it’s the expression 6 ÷ 2(1+2) and the author is asking if it evaluates to 1 or 9.
Upon first glance, you might think it evaluates to 1: one plus two is three, times two is six, and six divided by itself is one. But if you read the section above, you might say “Hey, wait a second… The order of operations (or ‘operator precedence’, if you were channeling Mrs. Alves, my Algebra I teacher) matters. So, I should evaluate the (1+2) first, net it to 3, and then proceed left-to-right, since multiplication and division (“My Dear”) are of equal precedence”. This also assumes that you recall that no explicit operator between the first ‘2’ and the open parenthesis implies multiplication (this is important later). So, 6 divided by 2 (net 3), times 3, equals 9.
This is how I would have interpreted this expression. Again, to further illustrate the order of operations, and how parentheses supersede (or is it supercede?) everything, let’s looks at the same expression in Mathcad with no parentheses.
Let’s now look at it when the parenthetical expression is evaluated first, and then the remainder of the expression is evaluated right to left – a common trap when the parenthetical expression is at the end.
This might be how some people arrive at ‘1’ as a solution.
But the underlying issue in this brainteaser is the implicit multiplication. I traded some messages with Mathcad’s Director of Software Development, Jakov Kucan about this topic. The reason I dug a little was because of this:
Examining this might elicit a response of “Wait a minute – didn’t you say above that the answer should net out to ‘9’? Why is Mathcad saying ‘1’ here?” The answer lies in how Mathcad delicately handles implicit multiplication. Says Jakov: “[As you know,] Mathcad has ‘implicit’ multiplication a.k.a. scaling. [From an operator precedence perspective, that] binds tighter than division.” In other words, Mathcad parses the expression a bit differently, and assigns a higher order of precedence to implicit multiplication than explicit multiplication or division. And that’s OK, as long as you know that’s how it works. But, to be safe, recall the order of operations processes parentheses first, so one could always re-write the expression with parentheses first to group out a term.
Note here that the multiplication is now explicit and not implicit, thus clearly denoting not only to Mathcad but also to the human reader which terms should be processed in which order.
Jakov agrees: “[By the way], this is a good example why in-line division as in 6/2*3 [isn’t the best] way to write an equation. Mathcad supports in-line division, but it’s a much better practice to use fractions, as in
He closes with: “Or, of course, use parentheses.” Oh, Aunt Sally….