Least Common Multiple

Find the least common multiple (LCM) of given n integers.

Input
n
a1 a2 ... an
n is given in the first line. Then, n integers are given in the second line.

Output
Print the least common multiple of the given integers in a line.

Constraints
- 2 ≤ n ≤ 10
- 1 ≤ ai ≤ 1000
- Product of given integers ai (i = 1, 2, ... n) does not exceed 2^31-1

Sample Input 1
3
3 4 6

Sample Output 1
12

Sample Input 2
4
1 2 3 5

Sample Output 2
30

# Buggy version:
def gcd(a, b):
    if a < b:
        a, b = b, a
    while b:
        a, b = b, a%b
    return a

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

n = int(input())
A = list(map(int, input().split()))

ans = 1
for a in A:
    ans = (ans, a)
print(ans)


# Fixed version:
def gcd(a, b):
    if a < b:
        a, b = b, a
    while b:
        a, b = b, a%b
    return a

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

n = int(input())
A = list(map(int, input().split()))

ans = 1
for a in A:
    ans = lcm(ans, a)
print(ans)

# Buggy version:
import math
def sieve(n):
    p = [1] * (n + 1)
    p[0] = p[1] = 0
    
    for i in range(math.ceil((n + 1) ** 0.5)):
        if p[i]:
            for j in range(2 * i, len(p), i):
                p[j] = 0
    return p

def prime_factor(n):
    p = sieve(int(n ** 0.5))
    factor = []
    for pi in compress(count(0), p):
        while n % pi == 0:
            n //= pi
            factor.append(pi)

    if n != 1:
        factor.append(n)
    return factor

from collections import Counter
from sys import stdin
readline = stdin.readline

n = int(readline())
c =[Counter(prime_factor(ai)) for ai in map(int, readline().split())]
s = set()
for ci in c:
    s.update(ci.keys())

ret = 1
for i in [i ** max(ci[i] for ci in c) for i in s]:
    ret *= i
print(ret)

# Fixed version:
import math
def sieve(n):
    p = [1] * (n + 1)
    p[0] = p[1] = 0
    
    for i in range(math.ceil((n + 1) ** 0.5)):
        if p[i]:
            for j in range(2 * i, len(p), i):
                p[j] = 0
    return p

from itertools import compress, count
def prime_factor(n):
    p = sieve(int(n ** 0.5))
    factor = []
    for pi in compress(count(0), p):
        while n % pi == 0:
            n //= pi
            factor.append(pi)

    if n != 1:
        factor.append(n)
    return factor

from collections import Counter
from sys import stdin
readline = stdin.readline

n = int(readline())
c =[Counter(prime_factor(ai)) for ai in map(int, readline().split())]
s = set()
for ci in c:
    s.update(ci.keys())

ret = 1
for i in [i ** max(ci[i] for ci in c) for i in s]:
    ret *= i
print(ret)

# Buggy version:
def GCD(x,y):
    r = x%y
    if r == 0:
        return y
    else:
        return GCD(y,r)


n = int(input())
list1 = list(map(int,input().split()))
result = list1[0]
for i in list1[1:]:
    result = result*i/GCD(i,result)

print(result)

# Fixed version:
def GCD(x,y):
    r = x%y
    if r == 0:
        return y
    else:
        return GCD(y,r)


n = int(input())
list1 = list(map(int,input().split()))
result = list1[0]
for i in list1[1:]:
    result = result*i/GCD(i,result)

print(int(result))

# Buggy version:
def gcd(a,b):
    while b:a,b=b,a%b
    return a

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

input()
a=list(map(int,input().split()))
b=1
for i in a:
    b=lcm(b,i)
print(b)

# Fixed version:
def gcd(a, b):
    while b:
        a, b = b, a%b
    return a
    
n = int(input())
a = list(map(int, input().split()))

lcm = 1
for i in a:
    lcm = lcm*i//gcd(lcm,i)

print(lcm)