Modular Arithmetic Deep Dive

141 words
1 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Modular Arithmetic Deep Dive ## Extended Euclidean Algorithm Find $x, y$ such that $ax + by = \gcd(a, b)$: ## Chinese Remainder Theorem System: $x \equiv a_1 \pmod{n_1}$, $x \equiv a_2 \pmod{n_2}$ with $\gcd(n_1, n_2) = 1$. Solution: $x = a_1 M_1 y_1 + a_2 M_2 y_2$ where $M_i = n_1 n_2 / n_i$ and $y_i M_i \equiv 1...

Modular Arithmetic Deep Dive

Extended Euclidean Algorithm

Find x,yx, y such that ax+by=gcd⁡(a,b)ax + by = \gcd(a, b):
python
def extended_gcd(a, b):
    if b == 0:
        return a, 1, 0
    g, x1, y1 = extended_gcd(b, a % b)
    return g, y1, x1 - (a // b) * y1

Chinese Remainder Theorem

System: x≡a1(modn1)x \equiv a_1 \pmod{n_1}, x≡a2(modn2)x \equiv a_2 \pmod{n_2} with gcd⁡(n1,n2)=1\gcd(n_1, n_2) = 1. Solution: x=a1M1y1+a2M2y2x = a_1 M_1 y_1 + a_2 M_2 y_2 where Mi=n1n2/niM_i = n_1 n_2 / n_i and yiMi≡1(modni)y_i M_i \equiv 1 \pmod{n_i}. Example: Solve x≡2(mod3)x \equiv 2 \pmod{3}, x≡3(mod5)x \equiv 3 \pmod{5}. M1=5M_1 = 5, M2=3M_2 = 3. y1=5−1≡2(mod3)y_1 = 5^{-1} \equiv 2 \pmod{3}. y2=3−1≡2(mod5)y_2 = 3^{-1} \equiv 2 \pmod{5}. x=2(5)(2)+3(3)(2)=20+18=38≡8(mod15)x = 2(5)(2) + 3(3)(2) = 20 + 18 = 38 \equiv 8 \pmod{15}. Join Discord PreviousNumber TheoryNextAdvanced Logic & Induction
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.