1
$\begingroup$

would someone please help me with this problem. So earlier, I asked this question: Row and column algorithm

And got an answer: The row is :$$r = \left\lfloor \frac{n-1}{t} \right\rfloor + 1$$

This involves dividing by int. variables like in computer programming. However, I am wondering if there is a formula that does not involve dividing by int. variables and just uses modulo.

My attempt:

n - ((n-1)%t) + 1

Doesn't seem to work. Help would be appreciated!

$\endgroup$
1
  • 1
    $\begingroup$ I've attempted to make the title more informative, while staying true to the intent of the problem. Feel free to re-title if I've missed the point. $\endgroup$ Feb 27, 2013 at 23:57

1 Answer 1

1
$\begingroup$

$\left\lfloor\frac{n-1}t\right\rfloor$ is not (n-1)%t. It is int((n-1)/t).

If you really want to "just use modulo" you can apply the identity that says that $$\left\lfloor\frac ab\right\rfloor = \frac{a-a\bmod b}b,$$ which says that you can use (n-1-(n-1)%t) / t. But that seems a little bit silly.

$\endgroup$
4
  • $\begingroup$ Thank you! I didn't know the actual modulo identity. Hopefully I will learn a lot more when I start taking computer science courses. Is there a reference page where I can learn more about modulos? How is that formula derived and how does modulo actually work (the formula)? $\endgroup$ Feb 27, 2013 at 23:54
  • 1
    $\begingroup$ The real identity is that $a = b\cdot\left\lfloor\frac ab\right\rfloor + a\bmod b$. When you divide $a$ by $b$, there is a quotient, which is $\left\lfloor\frac ab\right\rfloor$, and a remainder, which is $a\bmod b$. You needed the quotient, not the remainder. $\endgroup$
    – MJD
    Feb 27, 2013 at 23:57
  • $\begingroup$ Would a mod b = a - b [a/b] ? So for instance. 5 mod 2 = 1 5 mod 2 = 5 - 2[5/2] = 0 ? Sorry, I'm just confused. Oh, I see. 5/2 = 2 ^Is there a formula for that? Or do we have to make some sort of assumption. Such as knowing how many times 2 goes into 5. $\endgroup$ Feb 28, 2013 at 0:10
  • $\begingroup$ $\left\lfloor\frac52\right\rfloor = 2$, so $5\bmod 2 = 5 - 2\left\lfloor\frac52\right\rfloor = 5 -2\cdot2 = 1$, as you said. $\endgroup$
    – MJD
    Feb 28, 2013 at 0:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .