2022-01-17 15:48:12 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""Algorithme d'Euclide
|
|
|
|
|
|
|
|
Calcule le PGCD de deux nombres.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
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-24 09:41:38 +01:00
|
|
|
calculate(a, b)
|
|
|
|
|
|
|
|
|
|
|
|
def calculate(a, b):
|
2022-01-18 11:24:40 +01:00
|
|
|
|
2022-01-24 17:46:16 +01:00
|
|
|
# Initialisation
|
2022-01-17 15:48:12 +01:00
|
|
|
r = ["r", a % b]
|
|
|
|
a = ["a", a]
|
|
|
|
b = ["b", b]
|
|
|
|
|
2022-01-24 17:46:16 +01:00
|
|
|
# Séquence
|
2022-01-17 15:48:12 +01:00
|
|
|
while True:
|
|
|
|
a.append(b[-1])
|
|
|
|
b.append(r[-1])
|
|
|
|
r.append(a[-1] % b[-1])
|
|
|
|
if r[-1] == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
print_table(a, b, r)
|
|
|
|
print("Le PGCD est", r[-2])
|
|
|
|
|
|
|
|
|
|
|
|
def print_table(a, b, r):
|
|
|
|
"""Représentation de l'algorithme"""
|
|
|
|
|
|
|
|
width = max((len(str(i)) for i in r))
|
|
|
|
|
|
|
|
for i in range(len(r)):
|
|
|
|
print(f"{a[i]:^{width}} {b[i]:^{width}} {r[i]:^{width}}")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|