1

I have a prime number $p$, an arithmetic progression starting at $a$ with common difference $d$. How to find the first index of a term in that arithmetic progression which is a multiple of the given prime number $p$?

Example: $$ a = 4,\quad d = 9,\quad p = 11. $$

The sequence is $4,13,22,31,\dotsc$; the first index such that the term is divisible by $p$ is $2$, because $22\bmod 2 = 0$.

If no such term exists, give answer as $-1$.

Brute force works but takes a lot of time. Is there any alternative approach?

egreg
  • 238,574
  • You want to solve the equation $a+xd \equiv 0 \ (p)$. So the answer is just $x = -ad^{-1} \in \mathbb{Z}/p\mathbb{Z}$, where $d^{-1}$ can be found with the generalized Euclidean algorithm. – Crostul Jul 30 '14 at 13:21

3 Answers3

6

The $k$-th term of the progression is $a+kd$, so you need to solve $$ a+kd\equiv0\pmod{p} $$ or $$ kd\equiv -a\pmod{p} $$ which has surely a solution whenever $d$ is not a multiple of $p$; in this case take $e$ an inverse of $d$ modulo $p$, so the equation becomes $$ k\equiv -ae \pmod{p} $$

In your example the inverse of $9$ modulo $11$ is $5$, as $9\cdot 5=45\equiv 1\pmod{11}$. So the solution is $$ k\equiv (-4)\cdot 5\equiv -20\equiv 2\pmod{11} $$ In general you find the inverse modulo $p$ with the extended Euclidean algorithm.

If $d\equiv0\pmod{p}$ there is no solution.

egreg
  • 238,574
2

I assume the interesting case $\gcd(d,p)=1,\;$ i.e. $d$ has in inverse mod p. You want to solve $a + xd = Ip$. Compute $x=-ad^{-1} \pmod p$ and then the index $I$ is $\frac{a+xd}{p}$. In your case $$9^{-1} \equiv 5 \pmod {11}$$ $$x=-ad^{-1}=(-4\times5) \equiv 2 \pmod {11}$$ $$I = \frac{4+2\times 9}{11}=\frac{22}{11} = 2$$

gammatester
  • 18,827
1

Hint $\ 11\mid 4+9x\!\iff\! {\rm mod}\ 11\!:\ 4+9x\equiv 0\! \iff\! 9x\,\equiv-4\!\iff\!x\,\equiv\, \dfrac{-4}9 \,\equiv\, \dfrac{-4}{-2} \,\equiv\, 2$

Remark $\ $ The familiar arithmetic of $ $ fractions $ $ is valid modulo $\,m\,$ as long as one restricts to fractions $\,a/b\,$ with denominator coprime to the modulus $\,m.\,$ Indeed, by Bezout's identity for the gcd, $\,(b,m) = 1\,\Rightarrow\,b\,$ is invertible $ $ mod $\,m,\,$ therefore the fraction has the unique denotation $\,x = a/b = ab^{-1}$ (the unique solution of $\,bx = a).\,$ The usual rules of fraction arithmetic remain true (as long as one restricts to such fractions), e.g. from a prior answer:

Hint $\ {\rm mod}\ 13\!:\ \dfrac{41}7 \equiv \dfrac{28}7 = 4\ \ $ by $\ \ 41\equiv 41\!-\!13 = 28$

Alternatively $\ \dfrac{41}{7}\equiv\dfrac{(-2)(-1)}{-6}\equiv \dfrac{-2}{-2}\dfrac{12}3\equiv 4\ \ $ by $\ \ \begin{eqnarray}41&&\equiv\ \ 2\\ 7 &&\equiv -6\end{eqnarray}$

Alternatively $\ \dfrac{41}{7}\equiv \dfrac{2}7\equiv \dfrac{4}{14}\equiv \dfrac{4}1\ $ by Gauss's Algorithm.

Such fraction twiddling (adding/subtracting the modulus from numerator or denominator till things divide or factor nicely) works well for small numbers. For larger numbers one can invert the denominator by the Extended Euclidean Algorithm, or Gauss's algorithm if the modulus is prime.

Beware $ $ A fraction with noninvertible denominator (not coprime to modulus) is not well-defined. For example, mod $\rm\:10,\:$ $\rm\:4\,x\equiv 2\:$ has solutions $\rm\:x\equiv 3,8,\:$ so the "fraction" $\rm\:x \equiv 2/4\pmod{10}\,$ cannot designate a unique solution of $\,4x\equiv 2.\,$ Indeed, the solution is $\rm\:x\equiv 1/2\equiv 3\pmod 5,\,$ which requires canceling $\,2\,$ from the modulus too, because $\rm\:10\:|\:4x-2\iff5\:|\:2x-1.\,$ Similarly ill-defined is the fraction $\,x \equiv 1/4\,$ since $\,10\mid 4x-1\,\Rightarrow 10n = 4x-1\,$ $\Rightarrow$ $\,4x-10 = 1\,$ is even, contradiction. Ring-theoretically, this may be viewed as a generalization of the fact that division by zero is not well-defined, i.e. division by a $\,\rm\color{#c00}{zero\!-\!divisor}\,$ is not well-defined (in a nontrivial ring), since if $\,\color{#c00}{ac=0,\ c\ne 0}\,$ then $\,ax = b\,\Rightarrow\,a(x\!+\!c) = b\,$ so if a solution $\,x\,$ exists then it is not unique.

Generally the grade-school rules of fraction arithmetic apply universally (i.e. in all rings) where the denominators are all invertible. This fundamental property will be clarified conceptually when one learns in university algebra about the universal properties of fractions rings (and localizations).

Bill Dubuque
  • 272,048