Magic Squares

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
12,939
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:

ABCDEFGH
11-Jan
2123456
378910111213
414151617181920
521222324252627
628293031

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

Array Formulas
CellFormula
B2: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.

ABCDEFGH
15
217241815#N/A#N/A
323571416#N/A#N/A
446132022#N/A#N/A
5101219213#N/A#N/A
611182529#N/A#N/A
7#N/A#N/A#N/A#N/A#N/A#N/A#N/A
8#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
CellFormula
B2: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:

ABCDEFGHI
15
217241815#N/A#N/A#N/A
323571416#N/A#N/A#N/A
446132022#N/A#N/A#N/A
5101219213#N/A#N/A#N/A
611182529#N/A#N/A#N/A
7#N/A#N/A#N/A#N/A#N/A#N/A#N/A#N/A
8#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
CellFormula
B2: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?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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.
 
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.
 

Forum statistics

Threads
1,213,533
Messages
6,114,179
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top