Programmers N개의 최소공배수

Solution

def solution(arr):
    answer = 1
    for n in arr:
        answer = lcm(answer, n)
    return answer


def lcm(a, b):
    return a * b / gcd(a, b)


def gcd(a, b):
    if b > a:
        a, b = b, a

    while (b > 0):
        temp = b
        b = a % b
        a = temp
    return a
  • 다른 사람 풀이

    • gcd 함수가 있었다.
from fractions import gcd


def nlcm(num):
    answer = num[0]
    for n in num:
        answer = n * answer / gcd(n, answer)

    return answer