Neat equations for computation

My programming has rarely delved into maths. I was wondering though if people in the bar have any equations which they like and can explain.

Here is an example, how would you get the sum of all the integers from 1...10 inclusive? Obviously you can use a quick loop, however;

1 + 10 = 11 2 + 9 = 11 3 + 8 = 11 4 + 7 = 11 5 + 6 = 11 6 + 5 = 11 7 + 4 = 11 8 + 3 = 11 9 + 2 = 11 10 + 1 = 11 === (sum the above) 110

As you can see 110 is going to be twice the number we are looking for so the sum of the integers between 1...10 inclusive is 55.

This gives the equation S = (n+1) x n / 2 where n is as big a positive integer as you like, a lot better than using a loop, and you can 'see' that it will work for all integers.

Anybody got any more neat examples with an explanation?

Magic square.jpg

Magic square of any size as long as the sides are an odd number.

See above illustration for a 5 * 5 square.

The 5 * 5 square in the middle is the actual magic square, the numbers around the outside are to illustrate how to create it.

Place 1 in the middle of the top row.

Move diagonally up and to the right, this takes you outside the square, I have put 2 where you end up. Jump to the bottom of the same column, I have put 2 where you end up. Go back into the square immediately above where you are, which is the bottom of column 4, where I have put 2.

Move diagonally up and to the right with 3. 4 takes you off the edge, same procedure, I've put 4 outside the square on the right where you end up. Jump to the left where I've put 4 and re-enter the square into column 1 row 3 where I have put 4.

Move diagonally up and to the right with 5. Now you are stuck because the next square has 1 in it already. Go underneath with 6.

Move diagonally up and to the right with 6, 7, 8 then 9 takes you off the edge again. I'll let you follow the rest of the numbers.

If you add up each row, column or diagonal the all total 65.
If you multiply the size of the magic square, 5 in this case, by the number in the middle, 13 in this case, you get the number that the rows, columns and diagonals add up to; 65.

Try it with other sizes of squares, they have to be odd numbers.

Magic square.jpg

They had one up the other week,

8% of 25 is the same as 25% of 8.

8/100 x 25 = (25 x 8) / 100 = (8 x 25) / 100

This works for any A% of B, you can pick the easier one to be the %.

edit: added code tags for the math to print right

My mother, a secretary in the '40s, taught me the "casting out nines" method of checking arithmetic calcs. No use for that method in programming, but interesting nonetheless. Uses "modulo" implicitly.

To check the result of an arithmetical calculation by casting out nines, each number in the calculation is replaced by its digital root and the same calculations applied to these digital roots. The digital root of the result of this calculation is then compared with that of the result of the original calculation. If no mistake has been made in the calculations, these two digital roots must be the same

It's easier than it sounds... :slight_smile: Example, to check addition:

Annotation 2020-02-19 121553.jpg

The method works because the original numbers are 'decimal' (base 10), the modulus is chosen to differ by 1, and casting out is equivalent to taking a digit sum. In general any two 'large' integers, x and y, expressed in any smaller modulus as x' and y' (for example, modulo 7) will always have the same sum, difference or product as their originals. This property is also preserved for the 'digit sum' where the base and the modulus differ by 1.

The earliest known surviving work which describes how casting out nines can be used to check the results of arithmetical computations is the Mahâsiddhânta, written around 950 by the Indian mathematician and astronomer, Aryabhata II (c.920–c.1000 ).[4] Writing about 1020, the Persian polymath, Ibn Sina (Avicenna) (c.980–1037), also gave full details of what he called the "Hindu method" of checking arithmetical calculations by casting out nines.

Source: Casting out nines - Wikipedia

Annotation 2020-02-19 121553.jpg

Start digging into Vedic math, it's amazing stuff from the people who gave us the zero.

Hi, Russian Multiplication.

Tom... :)

Russian Multiplication.

Tom… :slight_smile:

Binary at work.