Skip to content

gcd와 lcm #9

@sghong977

Description

@sghong977

왜 안되냐

`

"""
두 숫자를 소인수분해하면 되는거지 뭐
"""
import math

def get_insu(n):
insu = dict()
tmp = n
div = 2
while tmp != 1:
#if div > math.sqrt(n) + 1:
# break
# check if num is insu
if tmp % div == 0:
# yes
if div not in insu:
insu[div] = 1
else:
insu[div] = insu[div] + 1
tmp /= div
else:
div += 1
return insu

def solution(n, m):
answer = []

n_insu = get_insu(n)
m_insu = get_insu(m)

comps = set(n_insu.keys())
primes = comps.union(m_insu.keys())

gcd = 1
for p in primes:
    if p not in n_insu:
        continue
    elif p not in m_insu:
        continue
    else:
        cnt = min(n_insu[p], m_insu[p])
        gcd *= cnt * p
lcm = 1
for p in primes:
    if p not in n_insu:
        lcm *= m_insu[p] * p
    elif p not in m_insu:
        lcm *= n_insu[p] * p
    else:
        cnt = max(n_insu[p], m_insu[p])
        lcm *= cnt * p
    
answer = [gcd, lcm]
return answer

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions