diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index b73323e6..b2be029d 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -26,6 +26,7 @@ - [n-th Fibonacci number](recursion/nth_fibonacci_number.py) - [Recursive Insertion Sort](recursion/recursive_insertion_sort.py) - [Recursive Sum of n numbers](recursion/recursive-sum-of-n-numbers.py) +- [GCD by Euclid's Algorithm](recursion/gcd_using_recursion.py) ## Scheduling diff --git a/algorithms/Python/recursion/gcd_using_recursion.py b/algorithms/Python/recursion/gcd_using_recursion.py new file mode 100644 index 00000000..092d65d8 --- /dev/null +++ b/algorithms/Python/recursion/gcd_using_recursion.py @@ -0,0 +1,23 @@ +#EUCLIDS ALGORITHM + +def gcd(a, b): + if a<0: + a = -a + if b<0: + b = -b + if b==0: + return a + else: + return gcd(b, a%b) + + +#TIME COMPLEXITY - O(log(min(a, b))) +#EXAMPLES +#print(gcd(10, 2)) -> 2 +#print(gcd(30, 15)) -> 3 +#print(gcd(-30, 15)) -> 3 +#WORKING +#We are using the Euclidean Algorithm to calculate the GCD of a number +#it states that if a and b are two positive integers then GCD or greatest common +#divisors of these two numbers will be equal to the GCD of b and a%b or +#gcd(a, b) = gcd(b, a%b) till b reaches 0