nawerilike.blogg.se

Magic square code
Magic square code









magic square code

A magic square is an arrangement of distinct numbers in arithmetical progression in a square grid, where the numbers in each row, and in each column, and the numbers in the main and secondary diagonals, all add up to the same number. This app finds magic squares using a genetic algorithm. The "magic sum" of this square is 16,400.Project for Artificial Intelligence module (2016/1) | University of Santa Cruz do Sul (UNISC)Īuthors: Guilherme Sehn, Gabriel Bittencourt, Mateus Leonhardt The numbers in red are the ones that are in sequential order and the blue numbers are the reverse order numbers. The 32 by 32 magic square shown below was also created by using the "rectangle method". Starting from the top left and counting backwards from 64 to 1, fill in the blank cells of the square and you have the finished square. The red lines indicate where to place the numbers 1 through 64 if the number is within a rectangle.įollowing these rules, the partially-completed magic square will look like this: This time the n=8 square is divided into rectangles, each of which has a dimension of n/2 by n/4. This is similar to the previous two solutions but is much easier to understand and to memorize. Then we continue without filling in 58, 57, 56 and 55 but 54, 53, 52 and 51 are blank cells and do get filled in.Ĭontinuing in this way, until the bottom right cell is encountered, the magic square is now complete with each row, column and diagonal summing to 260. However, when we reach 62, 61, 60 and 59 we can fill in those numbers because those squares are blank. For example, we can't fill in 64 or 63 because 1 and 2 are in those cells. Starting at the top leftmost cell, while counting from 64 then backwards to 1, insert this number whenever a blank cell is encountered. (see illustration)Ĭounting from 1 to 64, starting from the top left and only filling in numbers that fall within the red squares (while leaving the others blank) produces this partially completed magic square: We will place these 2 by 2 squares along both diagonals of the square. For this method, we will divide up the square into smaller squares, each of which has a side equal to n/4. Let's try building an 8 by 8 square with a different approach. This method works perfectly but gets a bit confusing with the "mini-squares", the large square and the formula for each. Now, we fill in the square with the numbers from 1 through 16 but only for the squares that have an 'M' or 'L' in it and we leave the others blank.įinally, starting at the top left cell, counting backwards from 16, only fill in the blank cells and then the square is completed. Next, divide the center into a large square the size of which is n/2.įor a 4 by 4 square the size would be 2. (note the 4 red squares).įor an 8 by 8 the mini-squares are 2 by 2, for a 12 by 12 the mini-squares are 3 by 3 and so on. Looking at the diagram below, divide the square into four "mini-squares" - squares at the four corners, the size of each equals n/4.Ī 4 by 4 magic square has mini-squares that are 1 by 1.

magic square code

Unfortunately, the above method only works for the 4 by 4 square and so we'll have to learn another way for constructing doubly even magic squares of any size. That is to say, exchange 16 & 1, 6 & 11, 13 & 4 and 10 & 7, and you'll have a magic square totalling 34. Now "flip" the numbers in the diagonals (the red lines). In a 4 by 4 grid write the numbers 1 through 16 from left to right.

magic square code

Here is a simple, easy to remember way to make a 4 by 4 magic square. So, for the 4 by 4 magic square, each row, each column and both diagonals would sum to To determine the sum of any normal magic square we use the formula: Now, let's discuss the construction of doubly even magic squares, starting with the simplest, the 4 row by 4 column one. • singly even (even but not a multiple of 4 where n=6, 10, 14, 18, 22, etc.) So, now we'll discuss the construction of magic squares where n=4, 8, 12 and so on.Ī 4 by 4 magic square is a doubly even magic square, one of the three types of magic square. The term "doubly even" isn't very descriptive and all it means is a multiple of four.











Magic square code