Skip to content

Commit d0895a6

Browse files
committed
LCM USING GCD
1 parent 62651e1 commit d0895a6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pygorithm/math/lcm_using_gcd.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Author: Ashutosh Gupta
2+
# Created On: 8/17/2017
3+
# Time: 10:03 PM
4+
5+
6+
def gcd(x, y):
7+
"""
8+
Function to find gcm (greatest common divisor) of two numbers
9+
:param x: first number
10+
:param y: second number
11+
:return: gcd of x and y
12+
"""
13+
while y != 0:
14+
(x, y) = (y, x % y)
15+
return x
16+
17+
18+
def lcm_using_gcd(List):
19+
""" function to find LCM for given list of elements
20+
21+
:param List: List of which LCM is to be found out
22+
"""
23+
lcm = List[0]
24+
for element in List:
25+
lcm = lcm * element / gcd(lcm, element)
26+
27+
return lcm
28+
29+
def get_code():
30+
""" returns the code for the current class """
31+
import inspect
32+
return inspect.getsource(lcm_using_gcd)

0 commit comments

Comments
 (0)