- All Free Papers and Essays for All Students

Magic Square Generation

Autor:   •  August 20, 2012  •  Essay  •  352 Words (2 Pages)  •  922 Views

Page 1 of 2

69. Magic Square Generation


A magic square consists of a square grid with number of rows = number of columns = r, containing integers1,2,3,…, r


occupying the different cells of the grid, one integer per cell, such that the sum of integers ineach row= the sum of integers in each column = the sum of integers in each of the two diagonals. Wedescribe below an algorithm to generate a magic square of odd order r, r>=3:1. Empty all the cells of the square.2. Fill the middle cell of the top row with the integer 1. Repeat step 3 till the entire square is filled:3. The next integer in sequence is to be filled in an empty cell which is found as follows:A. From the cell that is just filled, go up by one row if feasible(ie if you are not in the first row);otherwisego to the corresponding cell in the last row. that is rth row.B. Go to the column on the left if feasible( ie if you are not in column 1); otherwise, go to thecorresponding cell in the last column, that is the rth column, of the grid.C. If the cell reached at the end of the above two steps is empty, the cell is to be filled with the nextinteger; otherwise, the cell vertically below (ie one row below) the cell that is just filled is guaranteedto be empty, and is to be filled with the next integer.


Function MgSq().

A VBA function named MgSq is presented below to create a magic square as per the above algorithm.The DIM statements provide for a square of a reasonable size.The function is invoked by highlighting a range of the spreadsheet with equal number of rows and columns,the number of rows and columns being odd, and typing in =MgSq(), followed by pressingcontrol+shift+enter keys.The function fills the above range with the magic square generated using


Download as:   txt (1.9 Kb)   pdf (56.5 Kb)   docx (10.3 Kb)  
Continue for 1 more page »