Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Modular.mdx #5098

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions content/4_Gold/Modular.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,21 @@ assert 2 * x % MOD == 1

We can also find modular inverses through Euclidean Division.
Given the prime modulus $m > a$ we have:
$m = k \cdot a + r$, where k = $floor(\frac{m}{a})$ and $r = m \mod a$. Then: $0 = k \cdot a + r \mod m \iff r = -k \cdot a \mod m
\iff r \cdot a^{-1} = -k \mod m \iff a^{-1} = -k \cdot r^{-1} \mod m$.

$$
m = k \cdot a + r
$$

where $k = \lfloor \frac{m}{a} \rfloor$ and $r = m \mod a$. Then:

$$
\begin{align*}
& 0 = k \cdot a + r \mod m \\
\iff{} & r = -k \cdot a \mod m \\
\iff{} & r \cdot a^{-1} = -k \mod m \\
\iff{} & a^{-1} = -k \cdot r^{-1} \mod m
\end{align*}
$$

Here is a short recursive implementation of the above formula:

Expand Down
Loading