Coding/Programmers
[코딩테스트 입문] 최대공약수와 최소공배수(Python3)
Soo_buglosschestnut
2023. 1. 11. 12:24
[코딩테스트 입문] 최대공약수와 최소공배수(Python3)
https://school.programmers.co.kr/learn/courses/30/lessons/12940?language=python3
math함수 썼음!
lcm은 작동안해서 함수 따로 작성!
import math
def lcm(a, b):
return a * b / math.gcd(a, b)
def solution(n, m):
answer =[math.gcd(n,m), lcm(n,m)]
return answer
유클리드 호제법으로도 최대공약수, 최소공배수 구할수 있다!
https://codingpractices.tistory.com/34
유클리드 호제법 다른사람 코드
def gcdlcm(a, b):
c, d = max(a, b), min(a, b)
t = 1
while t > 0:
t = c % d
c, d = d, t
answer = [c, int(a*b/c)]
return answer