PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'''
Pythonic way to calculate Greatest Common Divisor of two numbers using Euclidean Algorithm
'''
def gcd(a, b):
while a%b != 0:
a, b = b, a%b
return b
def lcm(a, b):
'''Least Common Multiple'''
return int(a * b / gcd(a,b) )
#algorithm works even if m < n
m = 8*7
n = 2*3*5*7
print ('numbers: {}, {}'.format(m,n))
print ('gcd: ' , gcd(m,n) )
print ('lcm: ' , lcm(m,n) )
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run