From 2480eacdcc5927933b9f2d6de8a9f93ecaa6687b Mon Sep 17 00:00:00 2001 From: Juan Antonio Date: Sun, 1 Oct 2017 14:50:45 +0200 Subject: [PATCH] Adding Euclidean GCD algorithm --- other/euclidean_gcd.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 other/euclidean_gcd.py diff --git a/other/euclidean_gcd.py b/other/euclidean_gcd.py new file mode 100644 index 000000000..13378379f --- /dev/null +++ b/other/euclidean_gcd.py @@ -0,0 +1,18 @@ +# https://en.wikipedia.org/wiki/Euclidean_algorithm + +def euclidean_gcd(a, b): + while b: + t = b + b = a % b + a = t + return a + +def main(): + print("GCD(3, 5) = " + str(euclidean_gcd(3, 5))) + print("GCD(5, 3) = " + str(euclidean_gcd(5, 3))) + print("GCD(1, 3) = " + str(euclidean_gcd(1, 3))) + print("GCD(3, 6) = " + str(euclidean_gcd(3, 6))) + print("GCD(6, 3) = " + str(euclidean_gcd(6, 3))) + +if __name__ == '__main__': + main()