Finding smallest integer to make the expression A+Bx divisible by another number K

58 Views Asked by At

I've come across this problem working on a special coordinate system that uses xy pairs belonging to $x = x_0+Cw$ and $y = y_0+Ch$

I sometimes perform checks to see if points are colinear and arrive at expressions in the form $x=3+12C$ and $y = \frac{6}{7}+\frac{24}{7}C$. The details are not essential, just that I need to find the least value of C that make $x$ and $y$ whole numbers. As it stands $x$ evaluates to a whole number for any C so I am focussing on the expression for $y$, which can be written

$$y=\frac{A}{K} + \frac{B}{K}C$$

$A$,$B$,$K$ are constants. And $C$ is the smallest positive integer $C > 0$ that makes the resulting expression a whole number ($y\in\mathbb{Z}$)

In the example $\frac{6}{7}+\frac{24}{7}C$ the best value for $C$ is $5$, but it's not obvious to me what we need to do to find it. I looked at modular arithmetic I think we can state $24C≡1(mod7)$. In my application the values for C may be quite large so a computational step or formula would be ideal.

1

There are 1 best solutions below

1
On BEST ANSWER

You are correct in that modular arithmetic is the way to go. Specifically, you want to find the minimum positive integer $C$ such that $$A+BC \equiv 0 \pmod K$$ or equivalently $$BC\equiv (-A) \pmod K$$

Letting $g = \gcd(B, K)$, if $A$ is divisible by $g$, then this is equivalent to $$\frac{B}{g} C \equiv \left(-\frac{A}{g}\right) \left(\mod {\frac{K}{g}}\right) \to C \equiv \left(\frac{B}{g}\right)^{-1} \left(-\frac{A}{g}\right) \left(\mod {\frac{K}{g}}\right)$$

If $A$ is not divisible by $g$, there is no solution.

Edit: For finding the modular multiplicative inverse, there are a couple different ways, detailed on the Wikipedia page.