2022-01-17 11:25:27 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""Algorithme d'Euclide étendu
|
|
|
|
|
|
|
|
Calcule le PGCD de deux nombres et trouve les coefficients de Bezout.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2022-01-17 13:00:58 +01:00
|
|
|
|
2022-01-17 12:45:09 +01:00
|
|
|
def main():
|
2022-01-17 13:20:37 +01:00
|
|
|
if len(sys.argv) == 3:
|
|
|
|
a = int(sys.argv[1])
|
|
|
|
b = int(sys.argv[2])
|
|
|
|
else:
|
|
|
|
a = int(input("a : "))
|
|
|
|
b = int(input("b : "))
|
2022-01-17 11:25:27 +01:00
|
|
|
|
2022-01-24 09:41:38 +01:00
|
|
|
calculate(a, b)
|
|
|
|
|
|
|
|
|
2022-03-14 15:34:43 +01:00
|
|
|
def calculate(a, b, quiet: bool = False):
|
2022-01-24 17:46:16 +01:00
|
|
|
"""Calcul des coefficients de Bezout et du PGCD
|
2022-01-18 11:24:40 +01:00
|
|
|
|
2022-01-24 17:46:16 +01:00
|
|
|
u est associé à a et v est associé à b, dans tous les cas. S'il faut échanger
|
|
|
|
les associations, il faut donc échanger a et b à l'appel de cette fonction.
|
2022-01-24 17:56:46 +01:00
|
|
|
|
|
|
|
Retourne pgcd, u
|
2022-01-24 17:46:16 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Initialisation
|
2022-01-17 12:45:09 +01:00
|
|
|
r = ["r", a, b]
|
|
|
|
u = ["u", 1, 0]
|
|
|
|
v = ["v", 0, 1]
|
|
|
|
q = ["q", "", a // b]
|
2022-01-17 11:25:27 +01:00
|
|
|
|
2022-01-24 17:46:16 +01:00
|
|
|
# Séquence
|
2022-01-17 12:45:09 +01:00
|
|
|
while True:
|
|
|
|
r.append(r[-2] % r[-1])
|
|
|
|
u.append(u[-2] - (q[-1] * u[-1]))
|
|
|
|
v.append(v[-2] - (q[-1] * v[-1]))
|
|
|
|
if r[-1] == 0:
|
|
|
|
q.append("")
|
|
|
|
break
|
|
|
|
q.append(r[-2] // r[-1])
|
|
|
|
|
2022-03-14 15:34:43 +01:00
|
|
|
if not quiet:
|
|
|
|
print_table(r, u, v, q)
|
|
|
|
print("Le PGCD est", r[-2])
|
|
|
|
print(f"Les coefficients de Bezout sont u = {u[-2]} et v = {v[-2]}")
|
|
|
|
print("au + bv = pgcd(a, b)")
|
|
|
|
print(f"{a} * {u[-2]} + {b} * {v[-2]} = {r[-2]}")
|
2022-01-17 12:45:09 +01:00
|
|
|
|
2022-01-24 17:56:46 +01:00
|
|
|
return r[-2], u[-2]
|
2022-01-24 09:46:18 +01:00
|
|
|
|
2022-01-17 13:00:58 +01:00
|
|
|
|
2022-01-17 12:45:09 +01:00
|
|
|
def print_table(r, u, v, q):
|
|
|
|
"""Représentation de l'algorithme"""
|
2022-01-17 13:00:58 +01:00
|
|
|
|
|
|
|
width = max((len(str(i)) for i in r))
|
|
|
|
|
2022-01-17 12:45:09 +01:00
|
|
|
for i in range(len(r)):
|
2022-01-17 13:00:58 +01:00
|
|
|
print(f"{r[i]:^{width}} {u[i]:^{width}} {v[i]:^{width}} {q[i]:^{width}}")
|
2022-01-17 12:45:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|