forked from NITSkmOS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
euclidean_gcd.py
36 lines (30 loc) · 1.24 KB
/
euclidean_gcd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def euclidean_gcd(first, second):
"""
Calculates GCD of two numbers using the division-based Euclidean Algorithm
:param first: First number
:param second: Second number
"""
while(second):
first, second = second, first % second
return first
def euclidean_gcd_recursive(first, second):
"""
Calculates GCD of two numbers using the recursive Euclidean Algorithm
:param first: First number
:param second: Second number
"""
if not second:
return first
return euclidean_gcd_recursive(second, first % second)
def main():
first, second = map(int, input('Enter 2 integers: ').split())
print('Division-based: GCD of {} and {} is: {}'.format(first,
second,
euclidean_gcd(
first, second)))
print('Recursive: GCD of {} and {} is: {}'.format(first,
second,
euclidean_gcd_recursive(
first, second)))
if __name__ == '__main__':
main()