Thursday, April 4, 2013

What is 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 * 0 + 1 ?







Keep Scrolling Down to See the Answer...











Keep Scrolling Down to See the Answer...










 The Interactive Ruby Shell Says the Answer is 12



>> 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 * 0 + 1
=> 12


The Bash Shell Says the Answer is 12



$ let "x = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 * 0 + 1";echo $x
12


Lex's Fancy Calculator App Says the Answer is 12


And my calculator application running in the iPhone Simulator is always right!




Conclusion

It's tempting the to use the old rule from grade school: Anything multiplied by 0 is 0 and add 1 to it to arrive at 1.

However, you'd be forgetting the rule of operator precedence, which says that given a series of addition and multiplication operations, perform the multiplication operation(s) first and then perform the addition. So, multiply this (1 * 0) to get 0. Then add up all the other 1's and you get 12.

References

http://en.wikipedia.org/wiki/Order_of_operations

Sponsor Ads(Please click it if you liked this article. Thanks!)

1 comment: