【每日一题20220609】最小公倍数

:mage:‍ 给定任意个正整数,请编写一个函数,求出这些整数的最小公倍数。

【示例】
输入:2,3,4
输出:12
解释:根据数学常识,2,3,4的最小公倍数是12。

题目难度:中等
题目来源:codewars-Least Common Multiple

def solution(*args)-> int:
    # your code here

assert solution(2,3,4) == 12
assert solution(2,5) == 10
assert solution(9) == 9
assert solution(2, 4, 27, 8, 3, 9, 24, 216, 288, 51288) == 1846368
def solution(*args) -> int:
    # your code here
    if 0 in args:
        return 0
    elif len(args) == 0:
        return 1
    elif len(args) == 1:
        return args[0]
    else:
        greater = max(args)
        while True:
            if len(list(filter(lambda x: greater % x == 0, args))) == len(args):
                lcm = greater
                break
            greater += 1
        return lcm


assert solution(0, 1, 2) == 0
assert solution(2, 3, 4) == 12
assert solution(2, 5) == 10
assert solution(2, 4) == 4
assert solution(9) == 9
assert solution() == 1

2,4

def solution(*args) -> int:
    if len(args) == 1:
        return args[0]
    sum = max(args)
    for a in args:
        if sum % a == 0:
            continue
        sum *= a
    min_cm = 0

    while (sum > 0):
        flag = True
        for a in args:
            if sum % a !=0:
                flag = False
                break
        if flag:
            min_cm = sum
        sum -= 1
    return min_cm
assert solution(2,3,4) == 12
assert solution(2,5) == 10
assert solution(9) == 9
assert solution(2,4) == 4

嗦嘎 感谢提醒

def solution(*args) -> int:
    size = len(args)
    idx = 1
    i = args[0]
    f = 0  # 给定初始化结果
    if size == 1:  # 如果长度为1,则最大公倍数为i
        return i
    while idx < size:
        j = args[idx]
        b = i if i < j else j
        a = i if i > j else j
        while a != 0:  # 使用欧几里得公式求两个数的最大公约数 b
            a, b = b % a, a
        f = int(i * j / b)  # 最小公倍数=两数相乘/最大公约数
        i = f  # 阶段性得出来最大公倍数用于与下一个数字进行计算最大公倍数
        idx += 1
    return f
#累加法
def solution(*args) -> int:
    if args == ():
        return
    if len(args) == 1:
        return args[0]
    mul = 1
    for i in args:
        mul *= i
    j = max(args)
    while max(args) <= j <= mul:
        flag = True
        for i in args:
            if j % i != 0:
                flag = False
        if flag == True:
            return j
        j += 1


assert solution(2, 3, 4) == 12
assert solution(2, 5) == 10
assert solution(9) == 9
assert solution(3) == 3

优化了下,CodeWars过了。 :partying_face:

def solution(*args) -> int:
    # your code here
    len_args = len(args)
    if 0 in args:
        return 0
    elif len_args == 0:
        return 1
    elif len_args == 1:
        return args[0]
    else:
        greater = max(args)
        nums = 0
        while nums < len_args:
            for i in args:
                if greater % i == 0:
                    nums += 1
                else:
                    greater += 1
                    nums = 0
                    break
        return greater


assert solution(0, 1, 0) == 0
assert solution(2, 3, 4) == 12
assert solution(2, 5) == 10
assert solution(2, 4) == 4
assert solution(9) == 9
assert solution(1, 2, 3, 4, 5) == 60
assert solution(2, 4, 27, 8, 3, 9, 24, 216, 288, 51288) == 1846368
assert solution(2, 4, 27, 8, 3, 9, 24, 216, 288, 51288, 0) == 0
assert solution() == 1

# 辗转相减法求最大公因数
def solution(*args) -> int:
    if len(args) == 0:
        return -1
    new_list = []
    for i, i_value in enumerate(args):
        for j_value in args[i + 1:]:
            tmp = 1
            mi = min(i_value, j_value)
            ma = max(i_value, j_value)
            while tmp != 0:
                tmp = ma - mi
                if tmp > mi:
                    ma = tmp
                else:
                    ma = mi
                    mi = tmp
            new_list.append(ma)
    mu, p = 1, 1
    for i in new_list:
        p *= i
    for i in args:
        mu *= i
    return mu / p

#上边的有点绕了,再简化
def solution(*args) -> int:
    if len(args) == 0:
        return -1
    new_list = []
    for i, i_value in enumerate(args):
        for j_value in args[i + 1:]:
            tmp = i_value
            while tmp != j_value:
                if tmp > j_value:
                    tmp = tmp - j_value
                else:
                    j_value = j_value -tmp
            new_list.append(j_value)
    mu, p = 1, 1
    for i in new_list:
        p *= i
    for i in args:
        mu *= i
    return mu / p

assert solution(2, 3, 4) == 12
assert solution(2, 5) == 10
assert solution(9) == 9
assert solution(3) == 3
assert solution() == -1
def solution(*args):
    li=sorted(args)
    boud = 1
    while 1:
        for i in li[1:]:
            if (li[0]*boud)%i!=0:
                break
        else:
            return li[0] * boud
        boud+=1
1 个赞
def solution(*args):
    mul, mul_param = 1, 1
    args = sorted(args)
    for j, v in enumerate(args):
        mul *= v
        for m in args[j + 1:]:
            n = v
            while True:
                r = m % n
                if r == 0:
                    mul_param *= n
                    break
                else:
                    m = n
                    n = r
    return mul / mul_param


assert solution(2, 3, 4) == 12
assert solution(2, 5) == 10
assert solution(9) == 9
assert solution(3) == 3
def solution(*args)-> int:
    # your code here
    ls = [i for i in args]
    if 0 in args:
        return 0
    elif ls == 0:
        return 1
    elif len(ls) == 1:
        return ls[0]
    elif len(ls) == 2:
        return ls[0]*ls[1]
    else:
        num = max(ls)
        a = 0
        while True:
            a+=1
            count_num = a*num
            l = 0
            for i in ls:
                if count_num % i ==0:
                    l+=1
                    if l ==len(ls):
                        return count_num
if __name__ == '__main__':
    assert solution(2,3,4) == 12
    assert solution(2,5) == 10
    assert solution(9) == 9
    assert solution(2, 4, 27, 8, 3, 9, 24, 216, 288, 51288) == 1846368
def solution(*args)-> int:
    a = max([*args])
    while True:
        b=[a%i==0 for i in args]
        if set(b)=={True}:
            return a
        a = a + 1
1 个赞
def solution(*args):
    # your code here
    # 转换为可变列表
    l = [i for i in args]
    # 输入为空数据,则返回None
    if l == []:
        return None
    # 输入一个正整数,则直接返回该数据本身
    if len(l) == 1:
        return l[0]
    # 输入正整数数量大于等于2时,每次取出来两个取最小公倍数,并把取出来的数据从列表删除,同时把取出来两个数的最小公倍数输入列表中
    while len(l) >= 2:
        n, m = l[0], l[1]
        l.pop(0), l.pop(0)
        s = []
        tmp = 1
        j = 2
        while True:
            if n % j == 0 and m % j == 0:
                s.append(j)
                n, m = n // j, m // j
                continue
            if j > n or j > m:
                break
            j += 1
        for k in s:
            tmp *= k
        tmp *= n*m
        l.append(tmp)
    return l[0]
    if len(args) == 0:
        return 1
    if 0 in args:
        return 0
    if len(args) == 1:
        return args[0]

    init_v = args[0]
    for i in range(1,len(args)):
        vx=max_v = max(args[i],init_v)
        vn=min_v = min(args[i],init_v)
        while max_v % min_v != 0:
            max_v, min_v = min_v, max_v % min_v
        init_v = vx * vn // min_v
    return init_v

套用了思想 :laughing:

def solution(*args)-> int:
    max_num = max(args)
    while True:
        for num in args:
            #将最大数分别对列表中的元素取余
            if max_num % num != 0:
                max_num += 1
                continue
            if num == args[-1]:
                return max_num


assert solution(2,3,4) == 12
assert solution(2,5) == 10
assert solution(9) == 9
assert solution(2,4,27,8,3,9,24,216,288,51288) == 1846368
def solution(*args) -> int:
    res = 1
    while res >= 0:
        l = 0
        for i in args:
            if res % int(i) != 0:
                break
            else:
                l += 1
        if l == len(args):
            break
        res += 1
    return res


assert solution(2, 3, 4) == 12
assert solution(2, 5) == 10
assert solution(9) == 9
assert solution(2, 4, 27, 8, 3, 9, 24, 216, 288, 51288) == 1846368