Non-recursive implementation of extended Euclidean algorithm (Python)

0. Introduction

Purpose of this article

I couldn't understand the non-recursive implementation of the extended Euclidean algorithm used in ABC186-E, so I'll summarize what I researched and understood in this article. The extended Euclidean algorithm itself is omitted in detail because there were many easy-to-understand articles such as Extended Euclidean algorithm ~ How to solve the linear indefinite equation ax + by = c ~. A special feature on how to find "too much divided by"! ~ From the inverse element to the discrete logarithm ~](https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a#3-%E5%89%B2%E3%82%8A%E7%AE%97-a--b) summarizes the principle of non-recursive implementation of the extended Euclidean algorithm.

1. Extended Euclidean algorithm matrix representation

Suddenly, let's express the extended Euclidean algorithm in the form of a matrix.

ax + by = \gcd(a,b)

When solving

a = \lfloor\frac{a}{b}\rfloor b + r

As, the problem of $ (a, b) $ is replaced with the problem of $ (b, r) $ and solved. Expressing this as a matrix,

\begin{bmatrix}
a \\
b
\end{bmatrix}
=
\begin{bmatrix}
k & 1 \\
1 & 0
\end{bmatrix}
\begin{bmatrix}
b  \\
r
\end{bmatrix}

It will be. ($ \ Lfloor \ frac {a} {b} \ rfloor = k $). The extended Euclidean algorithm repeats this until it becomes $ (\ gcd (a, b), 0) $. Therefore, $ a = r_0, b = r_1, r = r_2, \ dots, \ gcd (a, b) = r_h $, and $ k_i = \ lfloor \ frac {r_ {i-1}} {r_i} \ rfloor If you write $, you can express it as follows.

\begin{bmatrix}
r_0 \\
r_1
\end{bmatrix}
=
\begin{bmatrix}
k_0 & 1 \\
1 & 0
\end{bmatrix}
\dots
\begin{bmatrix}
k_{h-1} & 1 \\
1 & 0
\end{bmatrix}
\begin{bmatrix}
r_h  \\
0
\end{bmatrix} \tag{1}

here,

\begin{bmatrix}
k_i & 1 \\
1 & 0
\end{bmatrix}

The inverse matrix of

\begin{bmatrix}
0 & 1 \\
1 & -k_i
\end{bmatrix}

Therefore, the $ (1) $ formula can be transformed as follows.

\begin{bmatrix}
r_h \\
0
\end{bmatrix}
=
\begin{bmatrix}
0 & 1 \\
1 & -k_{h-1}
\end{bmatrix}
\dots
\begin{bmatrix}
0 & 1 \\
1 & -k_0
\end{bmatrix}
\begin{bmatrix}
r_0  \\
r_1
\end{bmatrix} \tag{2}

Finally,

\begin{bmatrix}
x & y \\
u & v
\end{bmatrix}
=
\begin{bmatrix}
0 & 1 \\
1 & -k_{h-1}
\end{bmatrix}
\dots
\begin{bmatrix}
0 & 1 \\
1 & -k_0
\end{bmatrix} \tag{3}

If you put, the $ (2) $ formula will be

\begin{bmatrix}
r_h \\
0
\end{bmatrix}
=
\begin{bmatrix}
x & y \\
u & v
\end{bmatrix}
\begin{bmatrix}
r_0  \\
r_1
\end{bmatrix}

This $ (x, y) $ becomes the solution $ (x, y) $ of the opening $ ax + by = \ gcd (a, b) $.

2. Non-recursive representation of extended Euclidean algorithm

Consider an algorithm that actually finds $ (x, y) $ from the above matrix representation. Since it can be calculated based on the $ (3) $ formula, set the initial value to $ (x_0 = 1, y_0 = 0, u_0 = 0, v_0 = 1) $ (identity matrix).

\begin{bmatrix}
x_1 & y_1 \\
u_1 & v_1
\end{bmatrix}
=
\begin{bmatrix}
0 & 1 \\
1 & -k_0
\end{bmatrix}
\begin{bmatrix}
x_0 & y_0 \\
u_0 & v_0
\end{bmatrix}

When you calculate

\begin{array}{llll}
x_1 = u_0 \\
y_1 = v_0 \\
u_1 = x_0 - k_0u_0 \\
v_1 = y_0 - k_0v_0
\end{array}

As a program,

x -= k[0]*u
y -= k[0]*v
x,u = u,x
y,v = v,y

It will be an image like. All you have to do is repeat this,

def extGCD(a,b):
    x, y, u, v = 1, 0, 0, 1
    while b:
        k = a // b
        x -= k * u
        y -= k * v
        x, u = u, x
        y, v = v, y
        a, b = b, a % b
    return x, y

In this way, you can implement the extended Euclidean algorithm in a non-recursive way.

3. If you want to find only the modular reciprocal (eg ABC186-E)

Like ABC186-E written at the beginning, I think that the extended Euclidean algorithm is often used to find the modular reciprocal in the case of competitive pros. When $ (a, b) $ are relatively prime, the solution $ x $ of $ ax + by = 1 $ becomes $ ax \ equiv 1 (\ mod b) $, so the extended Euclidean algorithm is used to calculate the modular reciprocal. You can use the reciprocal method.

In this case, the value of $ (v, y) $ in the last program of 2. is not particularly required, so it can be calculated by the following program. (Some post-loop processing has been added to make $ x $ a positive value.)

def modinv(a, b):
    p = b
    x, y, u, v = 1, 0, 0, 1
    while b:
        k = a // b
        x -= k * u
        y -= k * v
        x, u = u, x
        y, v = v, y
        a, b = b, a % b
    x %= p
    if x < 0:
        x += p
    return x

Recommended Posts

Non-recursive implementation of extended Euclidean algorithm (Python)
Python implementation of non-recursive Segment Tree
Implementation of Dijkstra's algorithm with python
Euclidean algorithm and extended Euclidean algorithm
Comparison of R and Python writing (Euclidean algorithm)
Reproduce Euclidean algorithm in Python
Python implementation of particle filters
Implementation of quicksort in Python
Explanation and implementation of ESIM algorithm
Sorting algorithm and implementation in Python
Python implementation of self-organizing particle filters
Implementation of life game in Python
Implementation of desktop notifications using Python
Implementation of Light CNN (Python Keras)
Python algorithm
PRML Chapter 8 Product Sum Algorithm Python Implementation
Algorithm learned with Python 8th: Evaluation of algorithm
Machine learning algorithm (implementation of multi-class classification)
Explanation and implementation of Decomposable Attention algorithm
Python implementation of continuous hidden Markov model
Python memorandum (algorithm)
Algorithm learned with Python 13th: Tower of Hanoi
Basics of Python ①
Basics of python ①
Copy of python
Implementation of TRIE tree with Python and LOUDS
[Codeing interview] Implementation of enigma cryptographic machine (Python)
Explanation of edit distance and implementation in Python
Introduction of Python
[Python] Implementation of clustering using a mixed Gaussian model
Implementation example of simple LISP processing system (Python version)
Maximum likelihood estimation implementation of topic model in python
Python implementation comparison of multi-index moving averages (DEMA, TEMA)
A python implementation of the Bayesian linear regression class
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
Overview of generalized linear models and implementation in Python
Variational Bayesian inference implementation of topic model in python
A reminder about the implementation of recommendations in Python
[Python] Operation of enumerate
List of python modules
A * algorithm (Python edition)
Python basic grammar / algorithm
RNN implementation in python
ValueObject implementation in Python
Unification of Python environment
Copy of python preferences
Basics of Python scraping basics
[python] behavior of argmax
Genetic algorithm in python
Usage of Python locals ()
the zen of Python
Installation of Python 3.3 rc1
Implementation of Fibonacci sequence
Algorithm in Python (Bellman-Ford)
Relearn Python (Algorithm I)
# 4 [python] Basics of functions
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
Basics of python: Output
Installation of matplotlib (Python 3.3.2)