Python 라이브러리 - math

공식문서

source code

math

math.gcd

math.gcd() 함수를 이용하면 최대공약수(gcd, greatest common divisor)를 구할 수 있습니다.
math.gcd() 함수는 python3.5부터 사용할 수 있습니다.

import math 
gcd = math.gcd(50, 100, 70)
print(gcd)  # 10

위 코드처럼 50과 100, 70의 최대 공약수를 구할 수 있습니다.

math.lcm

math.lcm()은 최소공배수(lcm, least common multiple)를 구하는 함수입니다.
math.lcm()은 python3.9부터 사용할 수 있습니다.

import math
lcm = math.lcm(25, 40)
print(lcm)  # 200

위 코드처럼 25와 40의 최소 공배수를 구할 수 있습니다.

 

 

 

'Python > Python' 카테고리의 다른 글

Python 라이브러리 - fractions  (0) 2023.05.17
Python 라이브러리 - decimal  (0) 2023.05.16
Python 라이브러리 - enum  (0) 2023.05.16
Python 라이브러리 - pprint  (0) 2023.05.16
Python 라이브러리 - heapq  (0) 2023.05.16