SQ in silver chrome letters, a lab's gear stacked against the S: a rack, two oscilloscopes, pizza boxes and a six-pack. Strange Query Lab

CARDIAC › Tips and tricks › Silly math tricks

Silly math tricks

These tricks use strength reduction to take advantage of the fact that multiplying by 10 is cheap. On CARDIAC it's a single shift, so a trick that turns the hard part into a multiple of 10 is a real saving.

Squaring a number that ends in 5

Our first user-submitted tip comes from Brian MacKay, who writes: "To square a two-digit number ending in five, take the first digit, multiply it by the next bigger digit, then append 25 on the end." For 25, that's 2 times 3, which is 6, so 625. For 55, it's 5 times 6, which is 30, so 3025. It works all the way up to 95.

Here's why. A number ending in 5 is 10a + 5, and its square is 100 times a(a + 1), plus 25. The a(a + 1) is the first part, the 25 is the end, and putting one in front of the other is the times 100.

It suits CARDIAC better than you might expect. 55 squared is 3025, and that won't fit in a cell. But the trick hands you the answer in two pieces, 30 and 25, and each one fits fine, so the program just prints them one after the other. It finds the first digit with SFT 23, which cuts out the tens digit, and works out a times (a + 1) by adding, since CARDIAC can't multiply. That's nine passes at most, where squaring 95 by adding would take 95.

cell  contents  notes
 10      040    INP 40  cells 10-15: a, the tens digit of n, and a + 1
 11      140    CLA 40
 12      423    SFT 23
 13      641    STO 41
 14      200    ADD 00
 15      642    STO 42
 16      145    CLA 45  cells 16-25: a times (a + 1): add a + 1, a times
 17      741    SUB 41
 18      643    STO 43
 19      144    CLA 44
 20      242    ADD 42
 21      644    STO 44
 22      143    CLA 43
 23      200    ADD 00
 24      643    STO 43
 25      319    TAC 19
 26      544    OUT 44  cells 26-28: print a times (a + 1), then 25: n squared, in two pieces
 27      546    OUT 46
 28      900    HRS 00
 40      000
 41      000
 42      000
 43      000
 44      000    a times (a + 1)
 45      000
 46      025

Step through it in Cardigan

With 55 it prints 30 and 25.

Squaring a number just under 100

We hadn't seen this one before. For a number just below 100, work out how far below it is, take that away from the number, and put the distance squared on the end as two digits. 99 is 1 below, so 99 minus 1 is 98, and 1 squared is 01, which makes 9801. 97 is 3 below, so 94, then 09: 9409.

Why it works: if the number is 100 minus d, its square is 100 times (the number minus d), plus d squared.

On CARDIAC it comes out in two pieces too, 98 and 1 for 99, and squaring the distance takes at most nine passes of adding.

cell  contents  notes
 10      040    INP 40  cells 10-16: d, how far n is below 100, and n - d
 11      141    CLA 41
 12      740    SUB 40
 13      642    STO 42
 14      140    CLA 40
 15      742    SUB 42
 16      643    STO 43
 17      146    CLA 46  cells 17-26: d squared: add d, d times
 18      742    SUB 42
 19      645    STO 45
 20      144    CLA 44
 21      242    ADD 42
 22      644    STO 44
 23      145    CLA 45
 24      200    ADD 00
 25      645    STO 45
 26      320    TAC 20
 27      543    OUT 43  cells 27-29: print n - d, then d squared: n squared, in two pieces
 28      544    OUT 44
 29      900    HRS 00
 40      000
 41      100
 42      000
 43      000
 44      000    d squared
 45      000
 46      000

Step through it in Cardigan

With 99 it prints 98 and 1, which Cardigan's paper strip shows as +098 and +001.

Squaring by way of a multiple of 10

This one works for any number. Move the number down to a multiple of 10 and up by the same amount, multiply those two, and add the amount squared. For 17, go down 7 to 10 and up 7 to 24: 10 times 24 is 240, and 7 squared is 49, so 289.

Here's why. (n - c) times (n + c) is n squared minus c squared, so adding c squared back gives n squared, whatever c is. Picking c as the last digit makes n - c a multiple of 10, and multiplying by that is easy.

On CARDIAC, the easy part really is easy. For 17, the multiple of 10 is 1 ten, so the program adds 24 once and then SFT 10 makes it 240. Then it adds 7 seven times for the 49. That's at most nine passes for each digit, eighteen in all, where squaring 99 by adding would take 99. Both loops start at their test, so a 0 digit costs no passes at all.

The answer can be as big as 9801, and that won't fit in a cell, but the accumulator holds four digits. So the program builds the whole answer there, and STO keeps the last three digits, while SFT 03 leaves just the thousands digit to store separately. 17 prints 0 and 289, and 99 prints 9 and 801.

cell  contents  notes
 10      060    INP 60  cells 10-18: a and b, the digits of n, and n + b
 11      160    CLA 60
 12      423    SFT 23
 13      661    STO 61
 14      160    CLA 60
 15      433    SFT 33
 16      662    STO 62
 17      260    ADD 60
 18      663    STO 63
 19      169    CLA 69  cells 19-30: a times (n + b), by adding
 20      761    SUB 61
 21      700    SUB 00
 22      665    STO 65
 23      827    JMP 27
 24      164    CLA 64
 25      263    ADD 63
 26      664    STO 64
 27      165    CLA 65
 28      200    ADD 00
 29      665    STO 65
 30      324    TAC 24
 31      169    CLA 69  cells 31-42: b squared, by adding
 32      762    SUB 62
 33      700    SUB 00
 34      665    STO 65
 35      839    JMP 39
 36      166    CLA 66
 37      262    ADD 62
 38      666    STO 66
 39      165    CLA 65
 40      200    ADD 00
 41      665    STO 65
 42      336    TAC 36
 43      164    CLA 64  cells 43-51: ten times the first, plus the second, printed in two pieces
 44      410    SFT 10
 45      266    ADD 66
 46      668    STO 68
 47      403    SFT 03
 48      667    STO 67
 49      567    OUT 67
 50      568    OUT 68
 51      900    HRS 00
 60      000
 61      000    a
 62      000    b
 63      000    n + b
 64      000    a times (n + b)
 65      000
 66      000    b squared
 67      000
 68      000
 69      000

Step through it in Cardigan

With 17 it prints 0 and 289.

Multiplying by 11, and by 9, 12 and 8

To multiply a two-digit number by 11, write its digits a little apart and put their sum in the gap. For 72, that's 7, then 7 + 2 = 9, then 2, so 792. When the sum is 10 or more, carry the 1 into the first digit: 57 is 5, then 12, then 7, and the carry makes it 627.

Here's why. 11 times n is 10 times n plus n. Writing the digits apart is the times 10, and adding into the gap is the plus n, carry and all.

This is the one trick on the page that CARDIAC already knows. Because CARDIAC is decimal, SFT 10 multiplies by ten, moving every digit one place left. Then ADD puts the number back on top, and the adder takes care of the middle digit and the carry. That's three instructions, with no loop and no test, and it works for any n up to 90, as long as the answer fits in three digits.

cell  contents  notes
 10      040    INP 40  cells 10-15: ten times n, plus n
 11      140    CLA 40
 12      410    SFT 10
 13      240    ADD 40
 14      641    STO 41
 15      541    OUT 41
 16      900    HRS 00
 40      000
 41      000    n times 11

Step through it in Cardigan

With 57 it prints 627.

The same idea works for the numbers on either side of 11. Nine times n is ten times n minus n, so the program is the same with a SUB where the ADD was. Twelve times n adds n twice, and eight times n subtracts it twice. Each one costs an instruction more than its neighbor, and there's still no loop.

cell  contents  notes
 10      040    INP 40  cells 10-15: ten times n, minus n
 11      140    CLA 40
 12      410    SFT 10
 13      740    SUB 40
 14      641    STO 41
 15      541    OUT 41
 16      900    HRS 00
 40      000
 41      000    n times 9

Step through it in Cardigan

With 57 it prints 513. It works for any n up to 111.

Multiplying digit by digit

For a bigger multiplier, split it into its digits. 43 times 21 is 43 times 20 plus 43 times 1. The 20 is two lots of 43 times 10, which is 860, and adding 43 makes 903. Multiplying by 100 is just multiplying by 10 twice, so this handles a multiplier of any size with nothing but adding and the cheap times 10.

This is long multiplication, and it's how CARDIAC wants to multiply. The program works through y from its top digit down. Each round, it multiplies the product so far by ten with SFT 10, then adds x once for each unit in that digit. Taking the top digit off y is a nice CARDIAC move too. SFT 10 pushes it into the accumulator's fourth digit, and STO keeps only the last three, so it's gone.

Adding x to itself y times would take as many passes as y, which could be 999. Going digit by digit, it's never more than 27, nine for each digit. 1 times 999 takes 259 instructions instead of about 7,000.

The trick can also use subtraction. 109 is 100 + 10 - 1, so 52 times 109 is 5200 + 520 - 52. That works on CARDIAC as well, since subtracting costs the same as adding, but deciding when to do it costs a test for every digit, so this program just adds. The product has to fit in three digits, which rules out 52 times 109 here.

cell  contents  notes
 10      060    INP 60  cells 10-14: read x and y, and count three rounds, one for each digit of y
 11      061    INP 61
 12      169    CLA 69
 13      763    SUB 63
 14      664    STO 64
 15      162    CLA 62  cells 15-23: each round, multiply p by ten, and take the top digit off y
 16      410    SFT 10
 17      662    STO 62
 18      161    CLA 61
 19      413    SFT 13
 20      665    STO 65
 21      161    CLA 61
 22      410    SFT 10
 23      661    STO 61
 24      169    CLA 69  cells 24-35: add x to p, digit times
 25      765    SUB 65
 26      700    SUB 00
 27      666    STO 66
 28      832    JMP 32
 29      162    CLA 62
 30      260    ADD 60
 31      662    STO 62
 32      166    CLA 66
 33      200    ADD 00
 34      666    STO 66
 35      329    TAC 29
 36      164    CLA 64  cells 36-41: next round, then print p
 37      200    ADD 00
 38      664    STO 64
 39      315    TAC 15
 40      562    OUT 62
 41      900    HRS 00
 60      000    x
 61      000    y
 62      000    p, the product
 63      003
 64      000
 65      000
 66      000
 69      000

Step through it in Cardigan

With 43 and 21 it prints 903.

Dividing digit by digit

Division works the same way in reverse. To divide 903 by 43, scale 43 up by ten for as long as it still fits under 903, which gives 430. That goes in twice, leaving 43, so the first digit of the answer is 2. Scale back down to 43, which goes in once and leaves 0. The answer is 21.

On CARDIAC, that's one subtraction loop for each digit of the answer, with SFT 10 to scale the divisor up and SFT 01 to scale it back down. Subtracting y from x until it won't go any more would take as many passes as the answer, up to 999. Going digit by digit, it's never more than 27, so 999 divided by 1 takes 292 instructions instead of about 8,000.

The divisor only ever scales up as far as x, so it always fits in a cell, even when 100 times it wouldn't. To decide whether the next scale fits, the program works out ten times the divisor, minus x, minus 1, all in the accumulator, where four digits are room enough. The remainder comes for free, since it's whatever is left of x at the end. The divisor has to be more than 0.

cell  contents  notes
 10      060    INP 60  cells 10-14: read x and y; y starts as the divisor, and there's one round to do
 11      061    INP 61
 12      169    CLA 69
 13      700    SUB 00
 14      663    STO 63
 15      161    CLA 61  cells 15-19: while ten times the divisor still fits in x, scale it up and add a round
 16      410    SFT 10
 17      760    SUB 60
 18      700    SUB 00
 19      341    TAC 41
 20      162    CLA 62  cells 20-30: each round, make room for the next digit of q, and subtract the divisor while it goes
 21      410    SFT 10
 22      662    STO 62
 23      160    CLA 60
 24      761    SUB 61
 25      331    TAC 31
 26      660    STO 60
 27      162    CLA 62
 28      200    ADD 00
 29      662    STO 62
 30      823    JMP 23
 31      161    CLA 61  cells 31-40: scale the divisor back down; after the last round, print q and the remainder
 32      401    SFT 01
 33      661    STO 61
 34      163    CLA 63
 35      200    ADD 00
 36      663    STO 63
 37      320    TAC 20
 38      562    OUT 62
 39      560    OUT 60
 40      900    HRS 00
 41      161    CLA 61  cells 41-47: scale up: ten times the divisor, one more round
 42      410    SFT 10
 43      661    STO 61
 44      163    CLA 63
 45      700    SUB 00
 46      663    STO 63
 47      815    JMP 15
 60      000    x, then the remainder
 61      000    y, the divisor, scaled
 62      000    q, the quotient
 63      000
 69      000

Step through it in Cardigan

With 100 and 7 it prints 14 and 2.

Know a trick like these? Send it to nerd@strangequery.com and it might end up here.

All the tips.