Solving Another Math Meme! Or Not!
It came from Twitter.
This is very much the math equivalent of the dress meme. It isn’t like the last math meme I took on, where the form leads you down the garden path, making you think it means “Five (with excitement)” instead of “Five factorial”.
And I admit that I made unwarranted assumptions and was a bit more of a jerk in the thread than I wanted to be.
I mean, I still think I’m right, but I’m much more open to the idea that I’m wrong. But let’s start at the beginning.
8 ÷ 2(2 + 2)
What do we do next? The Order of Operations would argue that we start with taking care of the parens first.
2 + 2 = 4
8 ÷ 2(4)
8 ÷ 8
1
At least, this looks good to me, but there are folks saying “Wrong! Distributive property!”, which I think they mean to say it should be this way.
8 ÷ 2(2 + 2)
8 ÷ (2 * 2 + 2 * 2)
8 ÷ (4 + 4)
8 ÷ 8
1
Um … Could be? It certainly handles the ambiguity.
8 ÷ 2(2 + 2)
8 ÷ 2(4)
8 ÷ 2 * 4
4 * 4
16
Here, we get that multiplication and division are at the same level with PEMDAS, and so we read it left to right, and so 8 ÷ 2
gets handled before 2 * 4
.
And people are as convinced of their correct math as they were about the color of the Dress.
An aggravating aspect is that Twitter optimizes for brevity and intensity, so there’s less “This is correct because this principle” and a lot more “You don’t know this, so you would’ve failed elementary math”.
(Or, as appropriate, “maths”.)
I recall people plugging the equation into their fancy calculator of choice, and I recall seeing contradictory answers: some saying 1 and some saying 16. If the question has ambiguity to you, it has ambiguity to developers, and this means that different developers trying to make deadline will implement it different ways.
My personal take would be that 2(2+2)
is effectively (2 * (2 + 2))
, and so decoupling the 2
to make 8 ÷ 2
is wrong. I hold this as a largely uninformed and instinctual opinion.
I recall hearing (maybe in Data Structures?) that the solution is to add parentheses until all ambiguity is gone. Alternately, Reverse Polish Notation. Rather than A symbol B
, it always takes A B symbol
, searching through long equations until found. Going for the “equals 16” answer, we would rewrite this as follows.
8 ÷ 2(2 + 2)
8 2 ÷ 2 2 + * -> 4 2 2 + *
4 2 2 + * -> 4 4 *
4 4 * -> 16
And going the other way.
8 ÷ 2(2 + 2)
8 2 2 2 + * ÷
8 2 2 2 + * ÷ -> 8 2 4 * ÷
8 2 4 * ÷ -> 8 8 ÷
8 8 ÷ -> 1
Of course, for larger numbers, the it becomes harder to read.
As written, I believe the answer is 1, but with my skills, I cannot discount the other option, and believe that math equations, like programs, should be formatted until they are unambiguous.
I mean, parentheses are cheap.