November 19, 2009

Rapid Squares

Filed under: math — Joshua @ 10:19 am

There was a neat article in Wired recently about math shortcuts - you know, tricks to solve math problems quickly. I enjoy that stuff because it’s all about how you organize the numbers. Unfortunately, the article doesn’t make many of the tricks available, but there’s a list of user-contributed ones in the comments - and anyway, it’s not like the sourcecode is compiled. All you have to do to come up with these things, really, is play around with digits and variables.

Anyway, there was one posted in the comments that hadn’t occurred to me before. If you want to square a number that ends in five, the following works:

(1) separate the number into 5 and the prefix: i.e. 25 ⇒ 2, 5
(2) multiply the prefix by its successor: (2)(2 + 1) ⇒ 6
(3) slap 25 on the end: 6, 25 ⇒ 625

Neat, eh? This one caught my attention because it’s not immediately obvious why it works. OK, it’s pretty obvious where the 25 comes from, but not necessarily why the other part of the trick works. Fortunately, I can look at the assembly code.

For 25 it must go something like this:

(20 + 5)(20 + 5)

In general form, that’s

(x + y)(x + y) = x^2 + 2xy + y^2

25 is the y-squared, so the question is how you get

(prefix)(prefix + 1)

out of

x^2 + 2xy

Well, if you look twice there are three xs - and we know that x in this case is 20. So that implies we’re getting

(20*20) + 20(2*5)

from somewhere. And in the microsecond it takes you to realize that 2 times 5 is ten, the answer comes clear. The reason why this is a special trick that works with 5 is because we have a 10-based number system. It takes 10 of something to get you to shift a column, in other words. And 5 is half of what it takes to cause you to shift. So when you’re adding two of them together, what you’re really doing is just shifting one more value onto the prefix, which is why, rather than just squaring it, you multiply it by itself plus one more. Think of those trailing zeros in 20 and 10 not as values, rather, but just position markers.

(20*20) + 20(2*5) = (20*20) + 20(10)
(2-shift*2-shift) + 2-shift(1-shift)
2(2) + 2(1)
2(2 + 1)

In other words, whatever the prefix times whatever the prefix is plus one. Neat!

There’s no reason it can’t work for other numbers, of course. There’s just more record-keeping to do. If you wanted to do the trick for, say, 3, you’d have to keep in mind that you’re 4 off of shift.

(x + y)(x + y) = x^2 + 2xy + y^2
               = 20(20) + 2(20)(5-2) + (5-2)(5-2)
               = 20(20 + 2(5-2)) + 9
               = 20(20 + 10 - 4) + 9
               = 20(20 + 10) - 80 + 9

Which by the previous algorithm means just that you have to subtract out 80 from 600 and add 9 to it. In more general terms, it means you have to account for the distance from five, double that (since there are two factors of five), multiply it by the prefix (since there are that many members of the prefix missing), and then subtract that ammount shifted one column to the right (since this is the deficit that would enable us to shift if only the end number had been 5). For end numbers greater than five, obviously, you add.

So try it with a more complex example like 236.

We’re one up from five - so we’re going to have to add twice our prefix (shifted to the right) on to our result. The prefix is 23. We already know that 23 squared is 529 (or, if we don’t, we can apply the algorithm again). We need one more 23 (remember, it’s “prefix times prefix plus one - aka 24 23s in this case), so we’re at 552. We’ve got twice our prefix (46) to add back on - but shifted to the right, so 5520 + 46 = 5566. And then we add 36 (shifted to the right) to that to get 55696. Voila!

Yeah, so it’s not so magic when you’re off 5. But with practice it still saves time! The shortcut is: multiply however much “off shift” you are (which is double the distance from five) by the prefix, subtract (add if your trailing digit is greater than 5) that from 10 times the result of multiplying the prefix times its successor, add a zero, and then add the trailing digit squared.