Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2022-01-13 19:20:22

Hannibal lecter
Member
Registered: 2016-02-11
Posts: 392

algorithm for tiles a floor , very important to move on

Hi, I have this problem the solution it needs is not advanced not using "if and else statement" the book needs to see the answer with simple algebra.
I have the question and the solution but it's not understandable completely expect I know that we need mod 2 (ODD and even) in my solution.



A robot needs to tile a floor with alternating black and white tiles. Develop
an algorithm that yields the color (0 for black, 1 for white), given the row and
column number. Start with specific values for the row and column, and then
generalize

2022-01-14-101520.png

////// solution of the book start
Solution of the book :



Clearly, the answer depends only on whether
the row and column numbers are even or odd,
so let’s first take the remainder after dividing by 2. Then we can enumerate all expected
answers:


2022-01-14-101617.png


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 zero. We can achieve that by taking another
remainder operation:
color = ( (row % 2) + (column % 2) ) % 2

////// solution of the book Ended



pls explain the book solution only I want to understand it step by step I didn't understand what he mean by dividing
note that the dividing here is integer division ex : 3/2 = 1 not 1.5 because it discard the fractional part

I didn't understand the solution from beginning like " first take the remainder after dividding by 2"
which reminder? and what number we are dividing it by 2 ?
the solution is confusing be and ambiguous

Last edited by Hannibal lecter (2022-01-13 19:20:56)


Wisdom is a tree which grows in the heart and fruits on the tongue

Offline

#2 2022-01-13 20:51:25

Bob
Administrator
Registered: 2010-06-20
Posts: 10,626

Re: algorithm for tiles a floor , very important to move on

You need to devise a way to tell if COL + ROW = an EVEN number or an ODD number.  EVEN means a BLACK square. ODD means a WHITE square.

This calculation will achieve that:

total = col + row

value = total/2 - INT(total/2) where the division is a normal division with possibly a remainder, and INT yields the integer value of the answer ie. discards the fraction.

Look what happens when total is even and when it is odd.

total even: value = <another integer> - <same integer> = zero.  This happens because there is no fractional part to discard.

total odd: value = <an integer + 0.5> - <same integer without the 0.5> = 0.5.

So the value calculation enables you to detect ODD and EVEN as needed.

Bob


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#3 2022-01-13 22:03:09

Hannibal lecter
Member
Registered: 2016-02-11
Posts: 392

Re: algorithm for tiles a floor , very important to move on

so you mean for example :

for (1,1)

(1 % 2) + (1 % 2) = 1 + 1 = 1 white, is this the way I apply by numbers? am I correct
and please why the book in the end used additional mod :


color = ( (row % 2) + (column % 2) ) % 2


look he used again %2!! he found sum of reminders than take mod 2 again? why? and where locations should I apply this formula only in the last row? because he said "fourth entries" is he mean the last row, isn't enough to use only (row%2) + (col%2)

Last edited by Hannibal lecter (2022-01-13 22:08:17)


Wisdom is a tree which grows in the heart and fruits on the tongue

Offline

#4 2022-01-14 01:39:24

Bob
Administrator
Registered: 2010-06-20
Posts: 10,626

Re: algorithm for tiles a floor , very important to move on

I had to look up the use of % here as I'd not met that notation before.  Here's what I found:

Modulo is a math operation that finds the remainder when one integer is divided by another. In writing, it is frequently abbreviated as mod, or represented by the symbol %.

So % finds the remainder after dividing by 2.  So what I said and what the book says are the same.  I note that the book is also applying the mod to the row and column values before the total is found.  mod 2 division will 'detect' even and odd numbers .

The use of % more than once is necessary.  If you just had

color =  (row % 2) + (column % 2)

There are the four cases are 0 + 0 = 0;  0 + 1 = 1; 1 + 0 = 1; and 1 + 1 = 2

The last answer would not give a 1 or 0 so would generate an error.

But I think this would work:

color = ( row + column ) % 2

Perhaps you could try it.

There's a short page about modulo arithmetic here: https://www.mathsisfun.com/numbers/modulo.html

Bob


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

Board footer

Powered by FluxBB