0

I am looking for the modulo inverse of the following large exponential number that also has a large modulo:

$21^{10^{18}}$ mod $10^9$ + 7

I use the Euler's theorem: $a^{-1}$ mod n $\equiv$ $a^{\phi(n) - 1}$ mod n where $\phi(n)$ is the Euler's totient function.

Since n is a prime number in our case, $\phi(n) = n-1 $. Therefore, $\phi(10^9+7)$ = $10^9$ + 6

$(21^{10^{18}})^{-1}$ mod $10^9+7$ $\equiv$ $(21^{10^{18}})^{{10^9+6}-1}$ mod $10^9$ + 7

RHS = $(21^{10^{18}})^{10^9+5}$ mod $10^9$ + 7

I do not know how to further reduce the above large exponent. Any help is greatly appreciated!

Tze-Wo
  • 11

2 Answers2

2

There are ways to reduce this to a $\rm\color{#90f}{handful}$ of multiplications $\!\bmod p = 10^9+7.\,$ First, notice that $\!\bmod p\!-\!1 = 10^9+6\!:\ \color{#0a0}{10^9\equiv -6}\Rightarrow 10^{18}\equiv(\color{#0a0}{10^9})^2\equiv (\color{#0a0}{-6})^2\equiv 36,\,$ so by mod order reduction $\!\bmod p\!:\ 21^{10^{18}}\equiv 21^{36}$ has inverse $(1/21)^{36}.\,$ But it is very easy to compute inverses of small numbers like $21$ by employing the handy method $\,\rm\color{#c00}{IR} =$ inverse reciprocity, i.e.

$$\qquad\begin{align} \bmod 3\!:\,\ &p\equiv 1^9+1\equiv -1\\[.4em] \bmod 7\!:\,\ &p\equiv 3^9+0\equiv -1,\,\text{ by }\ 3^6\equiv 1\\[.3em] \overset{\rm\small CCRT}\Longrightarrow\ \bmod 21\!:\,\ &p\equiv \color{c00}{-1},\text{ thus }\bmod p\!:\ \dfrac{1}{21}\overset{\rm\color{#c00}{IR}}\equiv \dfrac{1+p}{21}\equiv 47619048 =: c \end{align}$$

so repeated squaring (a $\rm\color{#90f}{handful}$ of multiplications) $\,\Rightarrow\, \left[\dfrac{1}{21}\right]^{36}\!\!\!\equiv c^{36}\equiv 753568836\,$
viz. $\,c^{\large 36} = (c^{\large 2}c^{\large 2^{\Large 4}})^{\!\large \,2},\,$ via $\color{#90f}{\text{5 squarings + 1 multiplication}}$.

Bill Dubuque
  • 272,048
  • Follow the link for the idea and proof of the method $,\rm\color{darkorange}{IR} =$ inverse reciprocity $\ \ $ – Bill Dubuque Dec 06 '22 at 08:59
  • From what I understand, inverse reciprocity is very close to the extended Euclidean algorithm, which is also a simple way to invert $21$ modulo $10^9+7$. The Python pow() function nowadays hides all the maths. It is a black box. Try pow(21, -1, 10**9 + 7) – Gribouillis Dec 06 '22 at 09:26
  • @Gri Yes, if you follow my links you'll see that I remark here that IR can be viewed as optimization of the extended Euclidean algorithm (when it terminates in a couple steps). As you can see above, using IR it makes the inverse computable very easily via a couple minutes of purely mental arithmetic. – Bill Dubuque Dec 06 '22 at 10:12
  • 1
    @Gri Exercises like this are typically designed to teach and illustrate various ideas in elementary number theory (which would not occur if one instead resorts to brute-force machine calculation). I was one of the developers of Macsyma so of course I do know that computer algebra systems have algorithms for such (in fact I worked on that and related algorithms in Macysma). – Bill Dubuque Dec 06 '22 at 10:12
  • @BillDubuque Thank you for showing me 10^9 mod (10^9+6) is congruent to -6 and also the inverse reciprocity method! – Tze-Wo Dec 06 '22 at 15:15
1

Let $p = 10^9 + 7$. Euler's theorem (the same one you cited) tells us $21^{p-1} \equiv 1 \mod p$. Thus when raising $21$ to some huge power, we can reduce that exponent mod $p-1$ without changing the final result. It turns out that $10^{18} \equiv 36 \mod (p-1)$, so $21^{10^{18}} \equiv 21^{36} \mod p$, and the modular inverse of $21^{10^{18}}$ will be the same as the modular inverse of $21^{36}$.

Next we should use the same method you were starting to try before. The inverse of $21^{36}$ mod $p$ will be $\left(21^{36}\right)^{p-2}$ mod $p$.

From here, we could brute force the answer using $p-2$ multiplications on a computer, but $10^9$ operations may take awhile depending which language you use. Instead, we can speed up the process using the exponentiation by squaring algorithm, which lets us use around $\log_2(p)$ operations instead of $p$ operations.

Here's a short Python script that puts all these steps together.

p = 10**9 + 7
e = 10**18 % (p-1)  # aka 36
b = pow(21, e, p)  # b is 21^(10^18) mod p
result = pow(b, p-2, p)
print(result)

The final answer returned is 753568836. Note that in Python, pow(x, y, z) means $x^y \mod z$, and it runs fast because it incorporates "exponentiation by squaring" (or something similar). The code runs instantly on my laptop.

David Clyde
  • 5,251
  • Using this method I find $21^{36\times(p-2)} = 753568836 \mod p$ – Gribouillis Dec 06 '22 at 07:39
  • Yeah including that is a good idea. I edited my answer to add a quick Python script plus the final answer. – David Clyde Dec 06 '22 at 07:49
  • 1
    There is an even faster expression in Python >= 3.8, simply pow(21, -10**18, 10**9 + 7) – Gribouillis Dec 06 '22 at 08:12
  • @Gribouillis There's no need to do that huge exponentiation since $21$ is easiily invertible, so we can compute the answer by hand using only a handful of modular multiplications - see my answer. – Bill Dubuque Dec 06 '22 at 09:06
  • Also "It turns out that $10^{18}\equiv 36$" does not need machine calculation since it is a trivial consequence of basic congruence arithmetic as I explain in my answer. – Bill Dubuque Dec 06 '22 at 10:17
  • @DavidClyde Thank you for your detailed reply! I did not know the Euler's theorem can be applied to reduce the exponent 10^18 by taking the modulo of 10^9 + 6. I learnt something new today! – Tze-Wo Dec 06 '22 at 15:11
  • 1
    @DavidClyde Your Python script is useful too. I just used it to solve an almost the same problem that has a modulo of 10^9 + 3. Thank you again! – Tze-Wo Dec 06 '22 at 15:27
  • @Tze-Wo If we know any $,e,$ with $a^e\equiv 1\pmod{n}$ then we can reduce exponents on $a$ modulo $e,,$ e.g. we can choose $, e = \phi(n).,$ This is explained at length in the mod order reduction post linked in my answer. – Bill Dubuque Dec 06 '22 at 15:56
  • @BillDubuque Thank you! I will study the mod order reduction post. – Tze-Wo Dec 06 '22 at 17:36