Time for action - solving the first 4x4 magic square

While this puzzle could be solved by hand, it would take a considerable amount of time to do so. Since our deadline is approaching, we will use R to quickly make the necessary calculations.

To simplify our problem, let us first focus on the 4x4 square located in the top-left corner of the puzzle:

Time for action - solving the first 4x4 magic square

The top-left corner of Zhuge Liang's puzzle

Then follow these simple steps:

  1. Open R.
  2. On the first line in the R console, next to the greater than sign (>), type the following comment:
    > #first we will solve the top-left corner of the puzzle
    
  3. On the next line in the R console, type the following comment:
    > #by breaking it down into 2x2 squares and making sure that the sum of all rows, columns, and diagonals equals 130
    
  4. In the upper left-hand corner of the 4x4 square are three numbers. Since we know from Zhuge Liang's note that all 2x2 squares sum to 130, we can calculate the missing value at B1 as follows:
    > #the value for B1 is:
    > 130 - 1 - 62 - 35
    
  5. R will display the text [1] 32, which indicates that the value of your calculation is 32. Having solved for B1, you can now solve for the missing value in row B, B4. The calculation is as follows:
    > #the value for B4 is:
    > 130 - 32 - 35 - 34
    
  6. By working your way through each row, column, and diagonal of the 4x4 puzzle, you can solve the remaining cells in this section, as follows:
    > #the value for C2 is:
    > 130 - 62 - 35 - 2
    [1] 31
    > #the value for C1 is:
    > 130 - 31 - 61 - 2
    [1] 36
    > #the value for C3 is:
    > 130 - 36 - 31 - 33
    [1] 30
    > #the value for D4 is:
    > 130 - 1 - 35 - 30
    [1] 64
    > #the value for D3 is:
    > 130 - 61 - 2 - 64
    [1] 3
    > #the value for A3 is:
    > 130 - 34 - 30 - 3
    [1] 63
    > #the value for A4 is:
    > 130 - 1 - 62 - 63
    [1] 4
    

The completed puzzle section is pictured in the following diagram. All calculated cells have been highlighted:

Time for action - solving the first 4x4 magic square

Solution to the top-left corner of Zhuge Liang's puzzle

What just happened?

While solving the first quadrant of Zhuge Liang's puzzle, you encountered a number of R's fundamental elements.

Lines

Activity that takes place in the R console is divided into one line statements. Long statements will automatically wrap to fit the size of the R window, although they still occur on a single line in the console. For instance, the formulas:

> 1 + 1

and

> 1 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25

both occupy a single console line, in spite of the fact that the latter formula is wrapped to display on more than one line.

Lines that accept user input begin with a greater than sign (>), whereas static console-generated output lines do not. For example:

> date()
[1] "Sun Aug 31 08:00:00 234"

The first line accepted user input, whereas the second returned output from the R console.

You can press the Return (or Enter) key to move from one line to the next, or to commit your code, in the R console. R will always drop down a single line when the Return key is pressed. Previous lines will remain displayed in the console, however they will not be editable.

Comments

Each line that begins with a pound sign (#) in the R console is designated as a comment. We have used several comments thus far. For example, in the following code:

> #the value for A4 is:
> 130 - 1 - 62 - 63
[1] 4

The first line is a comment. As is customary in most programming languages, a comment can display any variety of text, code, or other allowable input. Since they are ignored by the console, comments are a useful and necessary tool for documenting and organizing your work.

We inserted several comments into our code in the previous activity. Without them, our console would have been filled with seemingly arbitrary calculations. Instead, our comments provided the context necessary for both others and ourselves to understand what we were calculating and why. It is recommended that you use comments at every relevant opportunity to make your code readable and easy to remember.

Note

Note that no mechanism for multiline comments currently exists in R. However, long comments will automatically wrap to the size of the R console window.

> #this is an exceptionally long comment that takes up the entire width of the R console, so it is automatically wrapped to display on a second line

Alternatively, lengthy explanations can be separated manually by splitting the text into several one line comments, like so:

> #this is an exceptionally long comment > #that has been manually split > #into several one line comments

Note that everything following the pound sign (#) on a given line is ignored by the R console. Therefore, it is possible to combine a comment and other code on the same line, so long as the comment comes last.

Calculations

At its core, R is a sophisticated calculator. We found the value of each missing cell in the 4x4 square using simple mathematical formulas. For instance, we used the following formula to find out the value of cell B1:

> 130 - 1 - 62 - 35

R can, of course, handle an endless variety of calculations. The most commonly used calculations, along with their symbols, are addition (+), subtraction (-), multiplication (*), and division (/). Using R to derive values in this fashion was just our first small step towards becoming familiar and comfortable with the R console.

Output

You may have noticed that some console lines do not begin with the greater than (>) sign. In our preceding activity, these lines contained the results generated by R. The output that R returned to our formula for cell B1 is just one example:

[1] 32

Any time that R displays information to us, it will not be editable and it will not begin with a special prefix. In contrast, all lines that we can edit will begin with the greater than sign (>).

Note

Note also that a [1] that appeared before each of our calculated values in the R console output. This is merely R's way of telling us that the result of our formula was contained in a single cell. R likes to think of data in terms of matrices with rows, columns, and cells, and will often prefix its output with such information. You can typically ignore this and only pay attention to the value(s) that you specifically requested.

Visualizing the R console

The following diagram contains a segment of the source code that we created while solving the initial 4x4 puzzle segment. Each comment, calculation, and output has been labeled to demonstrate the visual differences between these types of lines:

Visualizing the R console

Remember that all editable lines begin with a greater than sign (>). Of these, comments begin with a pound sign (#) and are used to document our code. Calculations consist of mathematical operations that we are conducting on our data. Output lines are generated by the R console, are not editable, and lack a leading greater than sign.

Pop quiz

  1. Which of the following characters appears at the beginning of each user editable line in the R console?

    a. =

    b. >

    c. -

    d. #

  2. Which of the following characters is used to begin a comment line in the R console?

    a. =

    b. >

    c. -

    d. #

  3. Which of the following actions submits a line of user input to the R console?

    a. Pressing the Tab key

    b. Pressing the Shift key

    c. Pressing the Return key

    d. Pressing the Escape key

Have a go hero

Using the techniques that we employed to solve the top-left quadrant of the puzzle, solve for the remaining cells of Zhuge Liang's 8x8 magic square. You should be able to accomplish this task in a short while as you get accustomed to the R console. Along the way, be sure to identify which console lines are comments, calculations, and outputs. Once finished, check that your row, column, and diagonal values add up to 260. Then, verify your solution with the completed puzzle in the following figure:

Have a go hero

Solution to Zhuge Liang's 8x8 magic square

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset