Thoughts Caused by level n magic problem

Source: Internet
Author: User
Preface to the question of level n magic

Recently, I have been studying some classic algorithms and want to change my mind. I rummaged through the old books in my house. I had no intention of discovering a book with a history of more than ten years in the Elementary School Mathematics Olympics. It's not easy to put it here. I looked at the directories, and the familiar questions were displayed in front of me again. I looked at them and turned them to the level n magic (which we didn't call at that time ). I remember that when I learned this question, I felt very interesting. Now I have read this article, so it was born. This article introduces the level n Magic Square with a large length, because this problem is quite interesting. However, the focus of this article is in section 2-thinking triggered by the level n phantom.

Directory
Section 1 level n magic problem
Section 2 Thoughts Caused by level n magic parties

Body

Section 1 level n magic problem


The so-called level n magic problem, commonly known as "horizontal and vertical oblique addition and Equality" (we called this at the time ). In terms of terminology, in a square table with N rows and n columns, there are 1, 2, 3 ...... N * N-1, N * n integers, and their diagonal, horizontal, vertical and number are equal.
Now, let's take a look at an example to familiarize yourself with the problem, as shown in:

It can be seen that there are two magic ways: Odd-order Magic Square and even-order Magic Square, and the even-order Magic Square is divided into 4 m Magic Square and 4 m + 2 magic square.

1. Odd-order magic
I remember there was a saying in the base level magic. With this saying, I was not afraid to go all over the odd magic square. In fact, this is also a step to implement an odd magic square.

Fantastic
First fill in the uplink center,
Please fill in them in Oblique order.
There is no top level for the upper level,
There is no Bottom Frame for the top frame. I will plot and explain this article. It's interesting to see if the graph is a little messy. I will not explain it in each step. If it seems boring to write 3*3 squares, you can try 5*5, 7*7 or bigger. After writing, check whether the horizontal and vertical values are equal.
Note: If you have any questions, please leave a message. Thank you!

OK. The odd magic is over. That's easy. Right to have fun!

2. An even-order Magic Square
To be honest, I have always thought that there is only one kind of even order magic, which is a 2 * N level magic problem. After checking this information, we can see that the even order magic has two sub-categories.

① 4 * N Level Magic Square
In fact, the generation of the 4 * N Level Magic Square is very simple. for the data on the diagonal lines in the square, the first line is a diagonal line (called a diagonal line) as the symmetric axis, and the second line is exchanged (called a diagonal line 2) and then use the second diagonal line as the symmetric axis to exchange the data of the first diagonal line. Simply put, assuming the matrix name is magicsquare, It is switching magicsquare [I, j] And magicsquare [n-1-i, n-1-j]. The old method is illustrated by plotting. The figure is as follows:
Okay, the 4 * N level magic is also late. How is it easy! Try it by yourself.

② 4 * n + level 2 Magic Square
4 * n + 2: at first glance, 4 * n is much more troublesome. The same is true, but its idea is also simple. Think of 4 * n + 2 as 2*(2 * n + 1), and then convert it into four 2 * n + 1 magic ways.
Note: The following uses level 6 magic as an example. Therefore, 4 * n + 2 = 6, so n = 1.

I describe the process of 4 * n + 2 Magic Square implementation by describing the method of adding each step.

Step 1: divide the entire table into four small tables (2 * n + 1) * (2 * n + 1) named A, B, C, and D. See

Step 2: in this way, the small tables A, B, C, and D become an odd magic square.
① Divide the numbers 1, 2,..., (2 * n + 1) * (2 * n + 1) into a, and implement an odd magic square for;
②. (2 * n + 1) * (2 * n + 1) + 1 ,..., 2*(2 * n + 1) * (2 * n + 1) these numbers are divided into B and an odd magic square is implemented for B;
③. 2*(2 * n + 1) * (2 * n + 1) + 1 ,... 3*(2 * n + 1) * (2 * n + 1) divide these numbers into C and implement an odd magic square for C;
④. 3*(2 * n + 1) * (2 * n + 1) + 1 ,... 4*(2 * n + 1) * (2 * n + 1) divide these numbers into D, and implement odd magic square for D.
See

Step 3: Start from the center of Table A (that is, the magicsquare [N] [N] in row N) and mark n numbers from left to right, other rows in Table A indicate the number in the leftmost N (marked with a red background in the figure ). Replace the numbers marked with the corresponding positions in Table C. See

Step 4: Start from the center (as explained above) in table B, mark the n-1 column from right to left, and exchange the data marked in Table B with the data at the corresponding position in table D. But in the Level 6 magic, N-1 is equal to 0 at this time, so B and d do not need to be exchanged.

So far, this magic is like.

Note: The program for the above issues will not be sent. If you are interested, you can write it yourself.

 

Section 2 is thought of by level n magic parties

The magic problem is over. By comparison, I still feel that the odd-order magic is more interesting and easier than the even-order magic. It should have ended, but I had a whim. The N-level magic could be solved using mathematical knowledge, so I would like to ask you: when you write programs to think about algorithms, do you use mathematical knowledge to solve the problem?

If this problem is implemented by programming within a limited period of time, if you have a good understanding of the N-level magic and know how to implement parity, then there is no problem. Congratulations! However, if you don't know the level n phantom very well or the parity magic, how can you solve this problem? Use enumeration or other algorithms. This is something you have to consider.

Speaking of this, I came up with another example, for example, to calculate the sum of 99,100,..., please implement programming. What do you do? This is really simple. I believe most of my friends will use one for or one while to solve the problem. Is there any simpler way? Of course there are, using mathematical knowledge to solve, 1, 2 ,..., 99,100 is an arithmetic difference series. The formula S = (first item + last item) X number of items/2 for the sum of the arithmetic difference series is used to obtain the result directly. Which is more efficient. Therefore, mathematical knowledge is very useful in programming, but we often do not think about it!

 

Section 3 Concluding remarks

If you have more interesting questions or questions worth pondering, please share them!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.