Add euclide.py
This commit is contained in:
parent
eea7992e43
commit
c382ff8408
2 changed files with 47 additions and 0 deletions
46
algebre-non-lineaire/euclide.py
Executable file
46
algebre-non-lineaire/euclide.py
Executable file
|
@ -0,0 +1,46 @@
|
|||
#!/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 : "))
|
||||
|
||||
# Initialisation
|
||||
r = ["r", a % b]
|
||||
a = ["a", a]
|
||||
b = ["b", b]
|
||||
|
||||
# Séquence
|
||||
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()
|
|
@ -33,6 +33,7 @@ def main():
|
|||
q.append(r[-2] // r[-1])
|
||||
|
||||
print_table(r, u, v, q)
|
||||
print("Le PGCD est", r[-2])
|
||||
|
||||
|
||||
def print_table(r, u, v, q):
|
||||
|
|
Loading…
Reference in a new issue