chore(Python) : add GCD using recursion following Euclid's Algorithm (#797)

pull/805/head
Hridyansh Pareek 2022-08-15 19:33:38 +05:30 committed by GitHub
parent d14ef69395
commit 6f19b452ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -26,6 +26,7 @@
- [n-th Fibonacci number](recursion/nth_fibonacci_number.py) - [n-th Fibonacci number](recursion/nth_fibonacci_number.py)
- [Recursive Insertion Sort](recursion/recursive_insertion_sort.py) - [Recursive Insertion Sort](recursion/recursive_insertion_sort.py)
- [Recursive Sum of n numbers](recursion/recursive-sum-of-n-numbers.py) - [Recursive Sum of n numbers](recursion/recursive-sum-of-n-numbers.py)
- [GCD by Euclid's Algorithm](recursion/gcd_using_recursion.py)
## Scheduling ## Scheduling

View File

@ -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