Odd magic square

July 2, References. Read article to me. To create this article, 46 people, some anonymous, worked to edit and improve it over time. This article has been viewedtimes. Learn more Magic squares have grown in popularity with the advent of mathematics-based games like Sudoku. A magic square is an arrangement of numbers in a square in such a way that the sum of each row, column, and diagonal is one constant number, the so-called "magic constant.

Next, start your square by placing the number 1 in the center box of the top row. Then, arrange the rest of the numbers sequentially by moving up 1 row, then 1 column to the right.

Learn why people trust wikiHow. Explore this Article methods. Tips and Warnings. Things You'll Need.

Odd Magic Squares in Java

Related Articles. Article Summary. Co-authored by 46 contributors Community of editors, researchers, and specialists July 2, References Read article to me.It's unclear who first came up with a magic square. There is a story about a huge flood in China a long time ago. The people were worried they would be washed away and tried to appease the river god by making sacrifices. Nothing seemed to work until a child noticed a turtle sporting a magic square on it's back that kept circling the sacrifice.

The square told the people how big their sacrifice needed to be in order to save themselves. Since then magic squares have been the height of fashion for any discerning turtle. Focus: Logic, ArraysMethods. In case you've never come across one before, a magic square is an arrangement of sequential numbers in a square so that the rows, columns, and diagonals all add up to the same number.

For instance, a 3x3 magic square is:. This programming exercise is concerned with creating odd sized magic squares i.

Magic Square

The trick with making such a square is to place the number 1 in the first row and middle column. To find where to place the next number, move diagonally upwards to the right i.

If such a move means you fall off the square, wrap around to the row or column on the opposite side. Finally, if the move takes you to a square that is already filled, go back to the original square and move downwards by one. Repeat the process until all the squares are filled.

Now the diagonal move upwards results in a square that is already filled, so we go back to where we came from and drop down a row:.

Magic square

Hint: Apart from the programming aspects of this exercise it's also a test of logic. Take each step of creating the magic square in turn and figure how it can be done with a two-dimensional array. Your program should have been capable of creating the 5x5 magic square below:.

Share Flipboard Email. Paul Leahy. Computer Science Expert. Paul Leahy is a computer programmer with over a decade of experience working in the IT industry, as both an in-house and vendor-based developer. Updated February 01, Level: Beginner. Each row, column and diagonal adds up to For example, a 3x3 magic square would start like so:. A move diagonally upwards means we wrap around to the bottom of the square:.

Likewise, the next diagonal move upwards means we wrap around to the first column:. The question is can your program create a 5x5 magic square like the one below? Here's my version:.The constant sum in every row, column and diagonal is called the magic constant or magic sumM. In this post, we will discuss how programmatically we can generate a magic square of size n. Before we go further, consider the below examples:.

Did you find any pattern in which the numbers are stored? In any magic square, the first number i. Let this position be i,j.

The position of next number is calculated by decrementing row number of previous number by 1, and incrementing the column number of previous number by 1.

At any time, if the calculated row position becomes -1, it will wrap around to n Similarly, if the calculated column position becomes n, it will wrap around to 0. If the magic square already contains a number at the calculated position, calculated column position will be decremented by 2, and calculated row position will be incremented by 1.

Attention reader! Writing code in comment? Please use ide. Three conditions hold:. Python program to generate. Fill the magic square. Printing magic square. To display output. This code is contributed. WriteLine "The Magic Square for ". WriteLine "Sum of each row or column ".

WriteLine. Improved By : Mithun Kumarrathbhupendra. Load Comments. We use cookies to ensure you have the best browsing experience on our website.A magic square whose rows and columns add up to a magic number but whose main diagonals do not, is known as a semimagic square. Composing functions cycleRows.

Line is a user defined modulo function, and helps calculate the addends for the number that will go in the current position. Generates an associative magic square.

Magic Square Party Trick - Numberphile

If the size is larger than 3, the square is also panmagic. The make-ms procedure allows to construct different magic squares for a same n, by modifying the grid filling moves. Defining the magic square as two applications of transpose. Encoding the traditional 'Siamese' method.

Finally, rotate each corresponding row and column of the table by the corresponding value in the list. Or, based on suggestion by Marios Mamzeris on the talk page which is similar but oriented different -- both rows and columns shifted somewhat :. See last column of version before moved to the top row. See here for the solution for all three cases. This REXX version will also generate a square of an even order, but it'll not be a magic square.

See here for all three cases. Create account Log in. Toggle navigation. Page Discussion Edit History. I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack. Magic squares of odd order From Rosetta Code.

Jump to: navigationsearch. Works with : GNU bc. Works with : Fortran version 95 and later. Works with : Free Pascal version 1.

Works with : Python version 3. Works with : Swift 5. Works with : Tcl version 8. Works with : Visual Basic. NET version Library: Wren-fmt. This page was last modified on 20 Juneat Privacy policy About Rosetta Code Disclaimers.These are often referred to as doubly-even magic squares. We will now look at magic squares that are of even order where the order is not a multiple of 4. These would have order 6, 10, 14, That is, numbers that are 2 times and odd number.

We can easily show that there are no 2 2 magic squares. The magic number would be 5 and no matter how we arranged the numbers 1, 2, 3, 4 in a square matrix, we cannot get all rows, columns and diagonals to sum to 5. Note each of the four numbers would be a corner and hence 1 would be a corner and would require three different numbers to sum with it and get which is impossible.

The following is a 6x6 magic square. We will examine the way this particular 6 6 magic square was generated. The are other methods, but as we will see they are not as straightforward as the methods we showed for odd ordered magic squares and doubly-even ordered magic squares.

However, the way the above square was generated is rather intriguing. First we partition the 6 6 matrix into four 3 3 blocks. In the upper left-hand block we enter the numbers 1,2,3, See the figure below. We then enter the numbers 10,11,12, Next we enter the numbers 19,20,21, Finally, we enter the numbers 28,29,30, However, the rows do not sum to In fact the first three rows sum to 84 and the last three sum to What can we do to correct this?Magic Squares of odd order by Marios Mamzeris. The following is a method of creating any odd-order associative magic square. I had the idea of this technique many years ago, back inand I finally decided to show it in public all rights reserved.

All associated magic square is self-complementary magic squares as well. The following three tables are depicting the creation of a 31x31 magic square. Then we make a left shift of all rows, skipping the middle one, starting with moving one number cell in the first row while moving an extra more number cell in each next row to n -1 numbers in the last n th row.

It starts with moving one number cell upwards from the left side while moving an extra more number cell in each next column to n -1 numbers in the last n th column, skipping the middle column this time.

A magic square is ready. That is the 3 rd table below, with light and dark grey cells and yellow diagonals. Initial table with all the numbers from 1 to n 2 in sequence The green arrow shows the direction of the cell shift that will follow and the green cells are the ones that will shift. First transformation primary cell shift. This table is the result of the shift depicted in green color above. The blue arrow shows the direction of the 2 nd and final cell shift and the blue cells are the ones that will shift.

Final transformation secondary cell shift that gives the Magic Square. An alternative way based on the above shift pattern is to create a magic square by performing the cell shifting in opposite directions for each half of the table, always skipping the middle row or column. Initial square with numbers 1 - n 2 in sequence. After the first pass. The Magic Square is ready. Some Notes Below are some simple notes to help the reader understand what is happening to the sums of rows and columns of the initial 1 table n x nand how each pass series of shifts affects them, and leads to the construction of a symmetrical magic square.

Each next row is following a decremental pattern of gains, and they all affect the row sums in a way that compensates the initial 1 sums to equalize with the middle row. In a similar fashion, we observe the same behavior in column sums.

Below we can see: On the left side, the squares with each cell value in relation to n. On the right side, the square progress with actual values. S quare 3 is the final and magic one. In the green-colored cells, the sums of each row and column. In the blue-colored cells, are the initial middle row and column cells, and their location throughout the passes.

Variations To get different Magic Squares for the same odd orderyou may swap the actions. For example, instead of making a Left-shift followed by an Up-shiftyou may try starting with a Right-shift followed by a Down-shift or another combination. Similarly for the alternative way shown above, you may swap the left-right dual-action per pass with right-left and the down-up with up-down.Tool to generate magic squares.

Magic Square - dCode. A suggestion? Write to dCode! Please, check our community Discord for help requests! Thanks to your feedback and relevant comments, dCode has developped the best Magic Square tool, so feel free to write!

Thank you! Set number 1 on the left of the median line, the other numbers are written following the rule: if the cell is empty, in the cell in the bottom right of the previous one, else directly to the left of the occupied cell. When the cell does not exist, go to the other side of the matrix. For a size 3x3, the minimum constant is 15, for 4x4 it is 34, for 5x5 it is 65, 6x6 it isthen, Any lower sum will force the use of either negative numbers or fractions not whole numbers to solve the magic square.

Kaldor's magic square is a square used in economics, which has nothing to do with digits or numbers of mathematics but rather with concepts from economic policy.

Need Help? Message for dCode's team: Thanks to your feedback and relevant comments, dCode has developped the best Magic Square tool, so feel free to write! Send this message. See also: Equation Solver — Number Partitions. Franklin's square is a panmagic square with a magic constant of Write a message Thanks to your feedback and relevant comments, dCode has developped the best Magic Square tool, so feel free to write!

What are the minimal possible sums magic values? What is the Franklin Square? What is the Lo-Shu Magic Square? What is the KaldorMagic Square? Paypal Patreon More. Using dCode, you accept cookies for statistic and advertising purposes.