Hello:
I have this problem:
To tile a floor with alternating black and white tiles, develop an algorithm that yields the color (0 for black and 1 for white), given the row and column number. And an image shows a checked square of 4 tiles by 4 tiles by four tiles starting with black in the first corner on the left. So black, white, black, white for the first row. And starting with white in the second row, second tile there is black, etc.
So I wrote:
row odd, col odd = 0
row odd, col even = 1
col even, row odd = 1
col even row even = 0
But the book I’m using gives a different answer:
Clearly, the answer depends only on whether the row and column numbers are even or odd, so lets first take the remainder after dividing by 2. Then we can enumerate all expected answers.
Row % 2 Column % 2 Color
0 0 0
0 1 1
1 0 1
1 1 0
In the first three entries of the table, the color is simply the sum of the remainders. In the fourth entry, the sum would be 2, but we want a 0. We can achieve that by taking another remainder operation:
color = ((row % 2) + (column % 2)) % 2
Thanks in advance for your comments.