Sunday, January 15, 2017

Magic Square Corollary

The following definition for a Magic Square series is from the EDU-BLOG:
In recreational mathematics, a magic square of order ‘n’ is an arrangement of n2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant.  A normal magic square contains the integers from 1 to n2. The term “magic square” is also sometimes used to refer to any of various types of word square.
The constant sum in every row, column and diagonal is called the magic constant or magic sum, M. The magic constant of a normal magic square depends only on n and has the value 
M = [n((n^2) + 1) / 2]  (Posted by Jerry Yoakum)
Thus the magic square series is like this: 1, 5, 15, 34, 65, 111, 175, 260…
Often when I want to practice programing I'll write some code to calculate some interesting mathematical number or series. Recently I picked the magic square series. When I finished I noticed that for each order of magnitude beyond n = 20 there was a pattern.

Magic Square Corollary

M(n) = M(20 * 10^y) = 4000 * 10^3y + 10 * 10^y  (Posted by Jerry Yoakum)
for y = 0 to ∞

n
M(n)
20 4,010
200 4,000,100
2,000 4,000,001,000
20,000  4,000,000,010,000
... ...


This was done solely for the enjoyment of playing around with some numbers. I used Roger's Online Equation Editor to make the above equation image. Very handy tool.

Next I noticed that any value of n = 10, 20, 30, ..., 90 can have the above equation applied to it. For example, n = 10:
M(10) = 505
Split 505 at the two least significant digits. Maintain the order of the left half to get 500 and 5. Then apply those values on the left and right of the plus sign, as so:
M(n) = M(10 * 10^y) = 500*10^(3y)+5*10^y
M(100) = 500,050
M(1,000) = 500,000,500
...

M(90) = 364,545
M(n) = M(90 * 10^y) = 3,645*10^(3y)+45*10^y
M(900) = 364,500,450
M(9,000) = 364,500,004,500
...

Saturday, January 07, 2017

Avoid Numerous Special Cases

Complexity is the enemy. (Posted by Jerry Yoakum)

There are often exceptional situations to an algorithm's design. Exceptional situations cause special cases to be added to the algorithm. Every special case makes it more difficult to debug, modify, maintain, and enhance an algorithm.

If you find too many special cases, you probably have an inappropriate algorithm. Rethink and redesign the algorithm.