# Why does this work?

Hi I need to know how the equation below works.

So I have a number line 0 to 21 each cell is divided into 3 units so 0-2 belongs to first unit, 3-5 belongs to the second unit and so on. This 3 unit cell is further divided into 2 unit cells. So what you want to determine is given a number within this line, which 2 unit block this number belongs to.

Example, if you have 10 10/3=3.333.... and so we know that 10 belongs to the 3rd block and now we need to determine where in this 3rd block 10 belongs to (remember the cell is further divided into 2 units) so the correct equation is ((int)(10/3)*2)/2 = 3 so we know that 10 belongs to the 3rd block first section. If you have 11, ((int)(11/3)*2)/2 = 3.5 so we know that this number belongs to the second block of the block 3. Could someone please explain why does ((int)(11/3)*2)/2 this equation works?

Thanks

• Note that (int) removes all the decimal point. You're converting a number to Java int type. – datauser Jun 3 '11 at 3:19

## 1 Answer

Suppose $N = 3a+b$, where $0 \leq b \leq 2$. So $N$ belongs to the $a$th cell. Your equation amounts to $$\frac{1}{2} \lfloor \textstyle \frac{2}{3} N \rfloor = \displaystyle \frac{1}{2} \lfloor 2a + \textstyle \frac{2}{3} b \rfloor = \displaystyle \frac{1}{2} \begin{cases} 2a & b \leq 1, \\ 2a+1 & b = 2 \end{cases} = \begin{cases} a & b \leq 1, \\ a + 1/2 & b = 2. \end{cases}$$ Here $\lfloor x \rfloor$ is the floor of $x$, which is the largest integer $m$ such that $m \leq x$ (this is equivalent to your int).

If $b \leq 1$ then $(2/3)b < 1$ and so $\lfloor 2a + (2/3)b \rfloor = 2a$. If $b = 2$ then $(2/3)b = 4/3 > 1$ and so $\lfloor 2a + (2/3)b \rfloor = 2a + 1$.