Python Algorithm Design - Extending Euclidean Algorithm

1. Extend the Euclidean algorithm

The Extended Euclidean Algorithm is one of the most classical algorithms in number theory, and its purpose is to solve indeterminate equations. It is used to solve a set of x, y with known a, b, so that they satisfy Bezou's equation:

ax+by = GCD(a, b)

What is an indeterminate equation?
Indeterminate equations ( Diophantine equations ) refer to equations or equations in which the number of unknowns is more than the number of equations, and the unknowns are subject to certain restrictions (such as requiring rational numbers, integers or positive integers, etc.).

Two, Python algorithm implementation


def gld(x, y):
    u0, v0 = 1, 0
    u1, v1 = 0, 1
    while y:
        q = x // y               #取整
        u0, u1 = u1, u0 - q * u1
        v0, v1 = v1, v0 - q * v1
        x, y = y, x % y
    print(x, u0, v0)

gld(2*3*7*9*11, 6*12*13)

output result
insert image description here

3. Author Info

Author: Xiaohong's fishing routine, Goal: Make programming more interesting!

Focus on algorithms, reptiles, game development, data analysis, natural language processing, AI, etc., looking forward to your attention, let us grow and code together!

Copyright Note: This article prohibits plagiarism and reprinting, and infringement must be investigated!

Guess you like

Origin blog.csdn.net/qq_44000141/article/details/130228521