40 + 40 x 0 + 1= ?
Short answer: 41Long answer: is this to be solved by parsing or by algebra? If it's to be solved by parsing, we need a set of parsing rules, in other words a convention. Grade school teaches things like BEDMAS/PEMDAS, but that's a fairly complex rule operating on groups. Instead let's go with one particular way of computer program parsing.
Read the first number until the operator is found. Create a tree leaf containing the operator, with a left branch containing the first number read, and a right branch being empty. Read the next symbol: if it's a parenthesis, start over but with the right branch becoming a new "leaf" to hold the next operator. If it's another number, put it into the right branch. Now simplify by applying the leaf operator to both its branches, and storing the result inside the leaf and clipping the branches. Read the next symbol, if it's an operator create a leaf with a left branch containing the resulting value previously computed and a right branch containing nothing... repeat.
So for the above expression: we read 40, plus, 40, we stop and add those to get 80, we read times, we read 0, we stop and multiply those to get 0, we read plus, 1, stop and add to get 1, and stop with the answer 1.
Since according to rules like PEMDAS the answer should be 41, we say the above expression was "malformed". You can get the right answer by adding parentheses in the right places, or you can use a more complicated parser that conforms to PEMDAS.
Why do we want PEMDAS's answer anyway? The answer comes from algebra. $$40 + 40*0 + 1 = x$$ is the algebraic question, which we must apply transformations to using algebraic rules. The particular rules are chosen in the same way that the particular parsing method is chosen--because we want the answer to be a particular something. In this case, we can use real-valued definitions and axioms of a vector space. The definitions describe addition and multiplication as operations on two numbers. The associativity axiom lets us rewrite as $$40 + 1 + 40*0 = x$$. Applying the definition of times, we rewrite as $$40 + 1 + 0 = x$$. Applying the definition of plus, we reduce to $$41 + 0 = x$$. Applying the definition of plus again, we reduce to $$41 = x$$.
Note that the definition of scalar multiplication implicitly takes precedence over addition. From $$40 + 1 + 40*0 = x$$, we might have said $$41 + 40*0 = 81*0 = 0 = x$$. But from the definitions, "40+1" constitute two distinct elements "40" and "1", while "40*0" does not--it constitutes one distinct element "x" in X, which in this case is "0". So the road to $$x=0$$ would be erroneous within our vector space axioms. The associative transformation above was also unnecessary since that multiplicative substitution takes precedence regardless (just like PEMDAS says), it's there just for fun.
Posted on 2011-10-15 by Jach
Permalink: http://www.thejach.com/view/id/211
Trackback URL: http://www.thejach.com/view/2011/10/40_40_x_0_1
The answer is actually 1. Anything multiplied by zero can't be multipled at all. Which is why all times tables start off with 1x2=2 :)
Not sure if serious or trolling... =P
Joe bloggs say 1, Mathematicians say 41!!! :(
40 + (40 x 0) + 1 = 41 - I think we can all agree on that. The parentheses guide us to the correct answer.
However, many people seem to be insisting that 40 + 40 x 0 + 1 (should also) = 41 ... because we should "assume" the parentheses, when no such indication exists.
It is clearly a malformed calculation and *should* include the parentheses for clarity. Having done so would have saved millions of characters from being typed into blogs, forums and social networks!! lol
Shaun - I agree entirely. Therefore without the brackets the answer is 1, you should never 'assume' anything. You should go off what is put in front of you. FACTS! How can you assume that there should be brackets there? Remember to ASSUME makes an ASS out of U and ME!
Shaun and John: math relies on assumptions, as does deductive logic in general. You say it's a malformed expression, every programming language disagrees with you.
For instance, if you type into Python:
>>> 40+40*0+1
41
And as my long answer explains, if you pick an axiom system you can still arrive at the correct answer without needing parentheses to clean up the expression. We only assume the axioms, we don't assume the parentheses--reread my post. (It's depressing that this is the most popular post I have and that it's still misunderstood.) I proved that the expression is not malformed in the vector space axioms (admittedly it could be a tighter proof), I could pick a different set of axioms as well. It's only a malformed expression when your parser gives you the wrong answer--the naive parser algorithm I outlined above does, but every actual programming language I've ever seen (and I've seen a lot) uses a more sophisticated parser to get the right answer.
Multiply, divide, add, subtract...that is the order if there is no parenthesis or brackets. Elementary school education.
Oh you. I wonder if there are any reading-comprehension-quiz programs out there to force people to pass before commenting...