From 8e271a60935b5a66c2553cbdf1c797ee350d70c8 Mon Sep 17 00:00:00 2001 From: Moiz Ahmed <43660046+MoizAhmedd@users.noreply.github.com> Date: Wed, 7 Aug 2019 20:36:12 -0400 Subject: [PATCH] Create euclideangcd.py --- .../src/euclidean/euclideangcd.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 code/code/mathematical_algorithms/src/euclidean/euclideangcd.py diff --git a/code/code/mathematical_algorithms/src/euclidean/euclideangcd.py b/code/code/mathematical_algorithms/src/euclidean/euclideangcd.py new file mode 100644 index 0000000..e164971 --- /dev/null +++ b/code/code/mathematical_algorithms/src/euclidean/euclideangcd.py @@ -0,0 +1,33 @@ +from __future__ import annotations +from typing import List + +def int_division(a:int,b:int)->List: + """ + Implementation of the Division Algorithm + -If a,b are elements of the naturals + -There exists a unique pair of integers q,r with q>=0 and 0<=r>> int_division(7,2) + [3,2,1] + >>> int_division(4,1) + [4,1,0] + """ + quotient = a//b + remainder = a%b + return [quotient,b,remainder] + +def euclidean_algorithm(a:int,b:int)->int: + """ + Implementation of the Euclidean Algorithm + >>> euclidean_algorithm(4,9) + 1 + >>> euclidean_algorithm(23814,8232) + 294 + """ + remainder = int_division(a, b)[2] + c = int_division(a, b)[1] + quotient = int_division(a, b)[0] + if remainder == 0: + return c + else: + return euclidean_algorithm(c, remainder)