chore(Python) : add GCD using recursion following Euclid's Algorithm (#797)
parent
d14ef69395
commit
6f19b452ea
|
@ -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
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue