Skip to content

Commit

Permalink
Merge pull request hackerearthclub#147 from melsener/lcm_gcd_py
Browse files Browse the repository at this point in the history
LCM and GCD for python added.
  • Loading branch information
iamjaspreetsingh authored Oct 16, 2018
2 parents 68667b7 + 299348d commit f84e0f1
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions SOLUTIONS/gcd_lcm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Python version of C++ Solution in GeeksforGeeks
def gcd(a,b):
if a == 0:
return b
return gcd(b%a,a)

def findGCD(_list,n):
res = _list[0]
for i in range(1,n):
res = gcd(_list[i], res)

return res

def findLCM(_list,n):
res = _list[0]
for i in range(1,n):
res = ((_list[i] * res) /(gcd(_list[i],res)))

return res


if __name__ == '__main__':
_l = [ 1, 2, 3]
n = len(_l)
res = findGCD(_l,n)
print "GCD= " + str(res)
res = findLCM(_l,n)
print "LCM= " + str(res)

0 comments on commit f84e0f1

Please sign in to comment.