Least Common Multiple

地址:http://www.codewars.com/kata/5259acb16021e9d8a60010af/train/python

 

题目:

Write a function that calculates the least common multiple of its arguments; each argument is assumed to be a non-negative integer.

 

代码:

def lcm2(a,b):
	m = max(a,b)
	n = min(a,b)
	if n == 0:
		return 0

	while(n>1):
		if m%n == 0:
			return a*b/n
		else:
			t = n
			n = m%n
			m = t

	return a*b/n
    
def lcm(*args):
	lenA = len(args)
	if lenA == 1:
		return args[0]
	elif lenA == 2:
		return lcm2(args[0],args[1])
	else:
		ans = lcm2(args[0],args[1])
		for i in range(2,lenA):
			ans = lcm2(ans,args[i])
		return ans

  

posted @ 2014-10-11 09:47  开开甲  阅读(273)  评论(0编辑  收藏  举报