Skip to content

Commit 161a983

Browse files
committed
Implementation for finding LCM of List of numbers
1 parent 96d03a6 commit 161a983

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pygorithm/math/lcm.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Author : OMKAR PATHAK
2+
# Created at: 16th August 2017
3+
4+
from functools import reduce # need this line if you're using Python3.x
5+
6+
def lcm(List):
7+
''' function to find LCM for given list of elements
8+
9+
:param List: List of which LCM is to be found out
10+
'''
11+
def _lcm(a, b):
12+
''' helper function for finding LCM '''
13+
if a > b:
14+
greater = a
15+
else:
16+
greater = b
17+
18+
while True:
19+
if greater % a == 0 and greater % b == 0:
20+
lcm = greater
21+
break
22+
greater += 1
23+
24+
return lcm
25+
26+
return reduce(lambda x, y: _lcm(x, y), List)
27+
28+
def get_code():
29+
""" returns the code for the current class """
30+
import inspect
31+
return inspect.getsource(lcm)

0 commit comments

Comments
 (0)