The World of Astoria Bob

Create and Solve Number Puzzles

Course Outline

January 23
Algorithms and Pseudocode
Krypto Card Game
Insumnia and Countdown Numbers

January 30
Magic Squares
Kakuro
Yohaku
Other Arithmetic Square Puzzles

February 6
Crossnumber Puzzles
Crossword/number Hybrids
Crossword Puzzles
Short Exploration of Other Puzzle Types


Notes for Week 2

3x3 MAGIC SQUARE GENERATOR

Working with ChatGPT, I (mostly the bot!) produced a simple script to output the 8 known (and related) 3x3 magic squares using the integers 1–9 once, using a brute force method of going through each of the 280K or so possibilities:

2 7 6
9 5 1
4 3 8
6 1 8
7 5 3
2 9 4
2 9 4
7 5 3
6 1 8
6 7 2
1 5 9
8 3 4
4 3 8
9 5 1
2 7 6
8 1 6
3 5 7
4 9 2
4 9 2
3 5 7
8 1 6
8 3 4
1 5 9
6 7 2

If you look closely, you'll see that the solutions are all related by rotation and/or translation, so that there is really only 1 unique solution to the 3x3 magic square. This works well for a 3x3 magic square, but a 4x4 has over a trillion possibilities, and 5x5 is astronomical, so plain brute force won't work for them. Other methods need to be employed that we won't go into here.

But magic squares (and yes, there are magic cubes and hypercubes as well!) don't need to limited to the nine non-zero base 10 digits. Using dcode.fr's awesome magic square solver, I was able to input just a few integers into a 6x6 magic square generator (which uses modified brute force!) and tell it to use 459 as the magic sum, and it produced this amazing fraction-packed magic square:

6x6 Magic Square

That's right, it uses negative numbers as well! So, just seeding the magic square generator with enough numbers to make the system of equations a little more bound and so easier to solve, we can generate magic squares with just about any magic sum, and explore patterns and possibilities way beyond the simple 3x3 square with only the integers 1-9. I'm not sure these puzzles have any practical application, but they're interesting nonetheless!


KAKURO

Originating evidently in the US and called Cross Sums, this is now the second most popular logic puzzle in Japan, and that's saying something! I'm including it here because it can be considered an arithmetic square puzzle, similar to magic squares, and almost the same as certain types of yohaku puzzles, which will be discussed below. Here's an example of a 4x4 kakuro puzzle from the Kakuro Online website:

Kakuro puzzle

To solve the puzzle, you need to find the the sums indicated in the grayed boxes across and down using only the digits 1-9. Digits can be used more than once in this puzzle, unlike sudoku, but a digit can't be used twice in the same row or column sum. Puzzles can get quite big and somewhat difficult, but the essence of the problem is the same as in magic squares, and so kakuro puzzles can be solved using brute force for small puzzles, and more sophisticated algorithms for larger versions. We'll solve similar puzzles below in the yohaku section, but we'll use this kakuro example in class to see how we do on paper.

Here's a Javascript function to solve this kakuro puzzle and the solutions it gave:

function solveEquations(lower, upper) {
  for (let a = lower; a <= upper; a++) {
    for (let b = lower; b <= upper; b++) {
      for (let c = lower; c <= upper; c++) {
        for (let d = lower; d <= upper; d++) {
          for (let e = lower; e <= upper; e++) {
            for (let f = lower; f <= upper; f++) {
              for (let g = lower; g <= upper; g++) {
                for (let h = lower; h <= upper; h++) {
                  for (let i = lower; i <= upper; i++) {
          if (a + b + c === 22 && d + e + f === 11 && g + h + i === 7 && a + d + g === 20 && a + d + g === 20 && b + e + h === 12 && c + f + i === 8) {
            console.log(`(${a}, ${b}, ${c}, ${d}, ${e}, ${f}, ${g}, ${h}, ${i})`);
          }
        }
      }
    }
  }
}
}
}
}
}
}

solveEquations(1, 9);

And the solutions are:

(7, 9, 6, 8, 2, 1, 5, 1, 1)
(7, 9, 6, 9, 1, 1, 4, 2, 1)
(8, 8, 6, 7, 3, 1, 5, 1, 1)
(8, 8, 6, 8, 2, 1, 4, 2, 1)
(8, 8, 6, 9, 1, 1, 3, 3, 1)
(8, 9, 5, 7, 2, 2, 5, 1, 1)
(8, 9, 5, 8, 1, 2, 4, 2, 1)
(8, 9, 5, 8, 2, 1, 4, 1, 2)
(8, 9, 5, 9, 1, 1, 3, 2, 2)
(9, 7, 6, 6, 4, 1, 5, 1, 1)
(9, 7, 6, 7, 3, 1, 4, 2, 1)
(9, 7, 6, 8, 2, 1, 3, 3, 1)
(9, 7, 6, 9, 1, 1, 2, 4, 1)
(9, 8, 5, 6, 3, 2, 5, 1, 1)
(9, 8, 5, 7, 2, 2, 4, 2, 1)
(9, 8, 5, 7, 3, 1, 4, 1, 2)
(9, 8, 5, 8, 1, 2, 3, 3, 1)
(9, 8, 5, 8, 2, 1, 3, 2, 2)
(9, 8, 5, 9, 1, 1, 2, 3, 2)
(9, 9, 4, 6, 2, 3, 5, 1, 1)
(9, 9, 4, 7, 1, 3, 4, 2, 1)
(9, 9, 4, 7, 2, 2, 4, 1, 2)
(9, 9, 4, 8, 1, 2, 3, 2, 2)
(9, 9, 4, 8, 2, 1, 3, 1, 3)
(9, 9, 4, 9, 1, 1, 2, 2, 3)

Now the task would be to plug these in and see if the conditions (no digit repeated in a row or column) are met. We can quickly eliminate some of these that have 2 digits the same in a row, so the resulting list would be:

(8, 9, 5, 8, 1, 2, 4, 2, 1)
(8, 9, 5, 8, 2, 1, 4, 1, 2)
(9, 7, 6, 7, 3, 1, 4, 2, 1)
(9, 8, 5, 7, 3, 1, 4, 1, 2)

Of the 4 solutions left, only one meets the kakuro conditions of no duplicate digits in a row or column. So the solution is:

(9, 8, 5, 7, 3, 1, 4, 1, 2)


YOHAKU

There is another type of puzzle, more recently developed, that looks like a 3x3 magic square, but the sums (or products) are not the same for each row and column, and it has no diagonal rules. The puzzle is called yohaku, and it uses actual math in creation and solving processes.

Let's start with a simple yohaku puzzle:

a b 12
c d 11
10 13 +

We can write a simple Javascript function to go through a range of numbers and pick out the ones that satisfy the four sums of the puzzle:

function solveEquations(lower, upper) {
  for (let a = lower; a <= upper; a++) {
    for (let b = lower; b <= upper; b++) {
      for (let c = lower; c <= upper; c++) {
        for (let d = lower; d <= upper; d++) {
          if (a + b === 12 && c + d === 11 && a + c === 10 && b + d === 13) {
            console.log(`(${a}, ${b}, ${c}, ${d})`);
          }
        }
      }
    }
  }
}

Since no limitations were set on the puzzle, any number can be in each box. While we could use fractions or other more difficult numbers, we will just show a possible set of solutions using the integers 1 through 9 by setting the "lower" function parameter to 1 and the "upper" function parameter to 9:

solveEquations(1, 9);

This gives the following results, with the format (a, b, c, d):

(3, 9, 7, 4)
(4, 8, 6, 5)
(5, 7, 5, 6)
(6, 6, 4, 7)
(7, 5, 3, 8)
(8, 4, 2, 9)

Notice the nice pattern of increasing and decreasing integers in the columns! Anyway, we have solved the yohaku with six of the infintely many possible solutions there are. The difference between this brute force method and just looking at the puzzle is that, like in the Krypto example, we are likely to stop when we find one solution. It gives more insight into the problem to find that there is pretty much an infinite set of solutions.

This example yohaku puzzle is from the website yohaku.ca, and is in the beginner section of addition puzzles. But again, the strength of the brute force method of solving this problem is that with just a small adjustment in the script, we can search over all the puzzles in this section of 2x2 puzzles. As long as the number of possibilities is not astronomical, the method works almost instantaneously, and can really give one some good insight into a range of problems. You can use this to design your own puzzles, and then have others solve them.


Here's another example of a yohaku from the website that involves multiplication:

a b c 48
d e f 50
g h i 36
40 72 30 x

The problem states that the solver can only use whole numbers (0, 1, 2, 3...), but doesn't say the solutions have to be mutually unequal, so we can have repeats. Here are all the solutions found by a simple Javascript script to iterate each variable from 0-10 in the format (a, b, c, d, e, f, g, h, i):

(1,  8,  6, 10,  1,  5,  4,  9,  1)
(2,  4,  6,  5,  2,  5,  4,  9,  1)
(2,  8,  3,  5,  1, 10,  4,  9,  1)
(2,  8,  3, 10,  1,  5,  2,  9,  2)
(4,  4,  3,  5,  2,  5,  2,  9,  2)
(4,  6,  2,  5,  2,  5,  2,  6,  3)
(8,  6,  1,  5,  2,  5,  1,  6,  6)

The problem statement on the website asks how many solutions you can find, which is rare in these problems, but the answer is 7 using 0 through 10. Since obviously 0 is not one of the answers, we can eliminate it from the script, and go higher and see if any additional solutions are found. Using 1-11, there are no additional solutions. If one uses 1-12, there are several new solutions with 12 in them. The testing program I used timed out during this last trial, so we'll just go with the 7 solutions we found using 0-10.

The founder of this type of arithmetic square has published several books of problems to solve. The dcode.fr website does not seem to have a solver for these types of puzzles, but the solutions are found by brute force algorithms on that website, so maybe in the future, they will have a solver.


OTHER ARITHMETIC SQUARES

Several teacher worksheet generation sites also have arithmetic squares on them. These simple puzzles are quite useful and popular in the earlier grades. Here's an example of a worksheet I found on a teacher peer site:

Addition Squares Example

If you look above at the simple addition yohaku example, it's very close to the worksheet just above. So yes, we could solve these with our handy brute force method, constraining the values we're looking for in any way we choose. As an exercise in class, we'll come up with some more interesting squares by allowing negative numbers and fractions.


GENERATING ARITHMETIC SQUARE PROBLEMS

We can reverse the process of solving these arithmetic squares and produce pretty much any problem we want. Let's do this with a few examples, and then for homework, you can produce a problem of your own for the class to solve next week, or whenever they get time.

Let's take a really interesting one, of unit fractions:

1/a 1/b 1/c u
1/d 1/e 1/f v
1/g 1/h 1/i w
x y z +

We could just take random values of each of the variables and then calculate the sums. Let's use only whole numbers between 2 and 12. While we could use a random number generator, we will just set the values to whatever ones we want, as this method will be used next week in creating crossnumber puzzles. So, for the following set of values of a-i, we can use the computer to solve the equations for u, v, w, x, y, and z, and then present a puzzle that has those values filled in and asks the solver to use only values of 2-12 for the variables in black:

a = 4, b = 5, c = 10, d = 8, e = 2, f = 12, g = 7, h = 3, i = 11

Using Wolfram Alpha, the answers are:

u = 11/20, v = 17/24, w = 131/231, x = 11/24, y = 31/30, z = 181/660

So, the puzzle to solve would be:

1/a 1/b 1/c 11/20
1/d 1/e 1/f 17/24
1/g 1/h 1/i 131/231
11/24 31/30 181/660 +

Values allowed for a-i would be limited to whole numbers between 2-12. The solver would be pretty good if they could figure this out quickly in their head or on a board or paper. But with a simple script, using a brute force algorithm, one could solve this problem on a computer fairly easily.