Skip to content

Commit 243b632

Browse files
authored
Merge pull request #32 from ashu01/master
LCM using gcd
2 parents 62651e1 + e097bcd commit 243b632

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-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)

tests/test_math.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
from pygorithm.math import (
44
lcm,
5+
lcm_using_gcd,
56
sieve_of_eratosthenes)
67

78
class TestLCM(unittest.TestCase):
89
def test_lcm(self):
910
self.assertEqual(lcm.lcm([3, 12, 16]), 48)
1011

12+
def test_lcm_using_gcd(self):
13+
self.assertEqual(lcm_using_gcd.lcm_using_gcd([3, 12, 16]), 48)
14+
1115
class TestSieveOfEratosthenes(unittest.TestCase):
1216
def test_sieve_of_eratosthenes(self):
1317
self.assertEqual(sieve_of_eratosthenes.sieve_of_eratosthenes(10), [2, 3, 5, 7])

0 commit comments

Comments
 (0)