250x250
Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

devlog_owen

python으로 최대공약수 구하기(프로그래머스 분수의 덧셈) 본문

algorithm/(python)프로그래머스

python으로 최대공약수 구하기(프로그래머스 분수의 덧셈)

developer_owen 2024. 7. 9. 14:27
728x90

 

 

기본적인 방법

def gcd(a, b):
    for i in range(min(a, b), 0, -1):
        if a % i == 0 and b % i == 0:
            return i

 

유클리드 호제법

 

def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a

# or

def gcd(a, b):
    if a % b == 0:
        return b
    elif b == 0:
        return a
    else:
        return gcd(b, a % b)

 

 

**math 라이브러리 사용

import math
a, b = 10, 15
math.gcd(a, b)  # 5

 

라이브러리가 개충격...

 

 

 

 

 

728x90