# Magic Squares



## Eric W (Jan 2, 2019)

This is not  a typical question thread, although I hope it may be of interest to those who play around with Excel for fun. It's somewhat in line with the "Tough Problem" series that ran a few years ago in this forum. People with spare time and interest might read on.

With the advent of the new array functions, I started thinking about some other existing array formulas and how they might change.  One such formula is how to create a monthly calendar with one formula. My favorite version I got from Barry Houdini in a post on this forum, although I've seen several versions around the internet:


ABCDEFGH11-Jan2123456378910111213414151617181920521222324252627628293031

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col></colgroup><thead>

</thead><tbody>

</tbody>*Calendar*​

*Array Formulas*
CellFormulaB2:H7{=IFERROR(DAY(TEXT(A1,"yyyy-mm-")&{2,3,4,5,6,7,8}+{0;7;14;21;28;35}-WEEKDAY(A1)),"")}

<thead>

</thead><tbody>

</tbody>*Entered with Ctrl+Shift+Enter.* If entered correctly, Excel will surround with curly braces {}.
*Note: Do not try and enter the {} manually yourself*

<tbody>

</tbody>


Just put the first of the month in A1.  I don't have the new functions yet, but my assumption is that you'd just need to enter the formula in B2 without CSE, and it would just spill into the other cells.


Another formula I came up with generates odd order magic squares.


ABCDEFGH15217241815#N/A#N/A323571416#N/A#N/A446132022#N/A#N/A5101219213#N/A#N/A611182529#N/A#N/A7#N/A#N/A#N/A#N/A#N/A#N/A#N/A8#N/A#N/A#N/A#N/A#N/A#N/A#N/A

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col></colgroup><thead>

</thead><tbody>

</tbody>*SEQUENCE*​

*Array Formulas*
CellFormulaB2:H8{=MOD(TRANSPOSE(ROW(INDIRECT("1:"&A1))+INT(A1/2))+(ROW(INDIRECT("1:"&A1))-1),A1)*A1+MOD(TRANSPOSE(ROW(INDIRECT("1:"&A1))*2-1)+ROW(INDIRECT("1:"&A1))-1,A1)+1}

<thead>

</thead><tbody>

</tbody>*Entered with Ctrl+Shift+Enter.* If entered correctly, Excel will surround with curly braces {}.
*Note: Do not try and enter the {} manually yourself*

<tbody>

</tbody>


If you change A1 to 3 or 5 or 7, it will generate a magic square of that size. If you create a larger selection when entering the formula, it will create larger squares. This is sometimes called the Siamese method of creation.



I don't have the new array functions yet, but my assumption is that the ROW(INDIRECT structures could be replaced with SEQUENCE. So I wrote up a UDF that I believe imitates SEQUENCE and came up with this version:


ABCDEFGHI15217241815#N/A#N/A#N/A323571416#N/A#N/A#N/A446132022#N/A#N/A#N/A5101219213#N/A#N/A#N/A611182529#N/A#N/A#N/A7#N/A#N/A#N/A#N/A#N/A#N/A#N/A#N/A8#N/A#N/A#N/A#N/A#N/A#N/A#N/A#N/A

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col></colgroup><thead>

</thead><tbody>

</tbody>*SEQUENCE*​

*Array Formulas*
CellFormulaB2:I8{=MOD(INT(SEQUENCE(A1,A1,(A1+1)/2,1+1/A1)),A1)*A1+MOD(INT(SEQUENCE(A1,A1,,2+1/A1)),A1)+1}

<thead>

</thead><tbody>

</tbody>*Entered with Ctrl+Shift+Enter.* If entered correctly, Excel will surround with curly braces {}.
*Note: Do not try and enter the {} manually yourself*

<tbody>

</tbody>


Much shorter.  If someone has the functions and can test that, I'd be interested to see if it really works.

To keep this post from getting too long, I'll cut it short now.  But there are all kinds of outstanding questions that follow from this:

1) The Wikipedia article on Siamese Method has an alternate method.  It would be interesting if it's amenable to the same type of formula.
2) There's a method that generates magic squares for doubly-even orders (4k, or 4, 8, 12, 16, etc.)  Different algorithm, but it's also workable in this type of formula.
3) Serious bonus points to anyone who can come up with a 4k+2 (6, 10, 14, etc.) method.  I've seen at least 3 different methods, but they all seem a bit too tough for an array formula.
4) More of an Excel question, but what's the best way to get rid of the #N/As?
5) I'm more or less limiting this inquiry to formulas, since there are a lot of VBA solutions, including some on this board.  But it might be interesting to see if anyone can come up with something succinct.

6) Does anyone have any other interesting array formulas, by which I mean a formula which actually generates a n X m array?  Or an idea for one?


----------



## RoryA (Jan 3, 2019)

Re #4 , the only way I know of would be to add an Index function wrapper so that each cell attempts to return an individual value, so you can use IFERROR to handle the resulting error.


----------



## Eric W (Jan 3, 2019)

RoryA said:


> Re #4 , the only way I know of would be to add an Index function wrapper so that each cell attempts to return an individual value, so you can use IFERROR to handle the resulting error.



I managed to use Conditional Formatting using ISERROR and a font color matching the sheet.  I also created a grid around it in a similar fashion.


For anyone wanting to take a shot at the first question, or to see how the existing formula works, put this formula in B10 and copy down and across:

=BASE(B2-1,$A$1,2)

You should see the pattern, and with some knowledge of bases be able to replicate it.


----------

