In how many distinct ways can I fill a grid with the integers 1 through 9 (each occurring exactly once) such that all cells in the grid are coprime with the neighbors? (Note that rotations and reflections count as distinct ways of filling the grid.)
Clarification : A cell's neighbors are the cells that share a full side with it. If two cells share a corner but not a side, they are not neighbors.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The only important things to consider are that even numbers (2, 4, 6, 8) may not be adjacent to each other, and similarly for the multiples of 3 (3, 6, 9). There is no other case where two numbers in the range 1-9 are not relatively prime.
Thus we can now start a massive case checking.
First, color the board with black and white like a chessboard, so the corners are black. All the even numbers must go in a single color. This can be verified by case checking again: if a white square is occupied (by an even number), there are three black squares that cannot be occupied; if two white squares are occupied, there are at least four black squares that cannot be occupied; if three white squares are occupied, all black squares cannot be occupied.
Case 1 : All even numbers go to white squares.
This case is much simpler. There are 4 ! ways to permute the even numbers. No matter how they are permuted, the three squares next to the 6 can only have the numbers 1, 5, 7 (the 3 and 9 cannot be adjacent to them); there are 3 ! ways to permute them. The 3 and 9 are for the other two squares, with 2 ! ways to permute. In total, there are 4 ! ⋅ 3 ! ⋅ 2 ! = 2 8 8 ways here.
Case 2 : All even numbers go to black squares.
This case has two more subcases, depending on whether the center square is an even number or not.
Case 2.1 : The center square is not an even number.
There are 4 ! ways to permute the even numbers to the corners. In any case, the two squares next to the 6 cannot be filled with 3 or 9 (adjacent and not coprime to 6), and the center cannot be either (adjacent to all the remaining empty squares, and hence the other of 3 and 9, and not coprime with that other number). There are thus 2 ! ways to permute the 3 and 9, and 3 ! ways for the 1, 5, 7 in the remaining spots. In total, this is 4 ! ⋅ 2 ! ⋅ 3 ! = 2 8 8 ways.
(Diagram: Squares marked x below cannot be 3 or 9.)
Case 2.2 : The center square is an even number.
The center square cannot be 6, as it's adjacent to four of the five remaining empty squares. So the 6 is on a corner. After that, this is, again, divided into two cases, depending on the location of 6 and the empty corner (the corner with an odd number):
Case 2.2.1 : 6 is across the empty corner.
There are 4 ways to select the position of the empty corner; this uniquely determines the position of the 6. There are 3 ! ways to permute the even numbers. Now, similar to above, the positions of 3 and 9 are also fixed; there are 2 ! ways to put them, and 3 ! for the 1, 5, 7, for a total of 4 ⋅ 3 ! ⋅ 2 ! ⋅ 3 ! = 2 8 8 .
3 and 9 cannot be on the squares marked x below.
Case 2.2.2 : 6 is not across the empty corner.
There are 4 ways to select the position of the empty corner, and 2 ways to select the position of the 6. There are 3 ! ways to select the other even numbers. This time, there are two possibilities for the positions of 3 and 9. The top O below is 3 or 9, and one of the remaining two O's is also 3 or 9, but either one works. That's a total of 2 ways to choose the positions, and 2 ! ways to permute 3 and 9. There are 3 ! more ways for the 1, 5, 7, for a total of 4 ⋅ 2 ⋅ 3 ! ⋅ 2 ⋅ 2 ! ⋅ 3 ! = 1 1 5 2 ways.
Totaling everything, we have 2 8 8 + 2 8 8 + 2 8 8 + 1 1 5 2 = 2 0 1 6 ways.