Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- torchserve
- 백준
- NLP
- pytorch
- Kubernetes
- datascience
- PytorchLightning
- vscode
- docker
- github
- Kaggle
- python
- FastAPI
- DeepLearning
- 코딩테스트
- Matplotlib
- GCP
- leetcode
- 완전탐색
- pep8
- 네이버AItech
- NaverAItech
- 알고리즘
- 프로그래머스
- rnn
- FDS
- wandb
- autoencoder
- GIT
- GitHub Action
Archives
- Today
- Total
Sangmun
유클리드 호제법(최대공약수), 최소공배수 본문
코딩테스트에서 종종 나오는 정수론 문제 관련한 코드를 정리해보고자 한다.
유클리드 호제법에 대한 설명
https://ko.wikipedia.org/wiki/%EC%9C%A0%ED%81%B4%EB%A6%AC%EB%93%9C_%ED%98%B8%EC%A0%9C%EB%B2%95
* Greatest common divisor(gcd)
def gcd(a, b):
while b > 0:
a, b = b, a % b
return a
* Least common multiple(lcm)
def lcm(a, b):
return a * b / gcd(a, b)
사실 둘다 python math 라이브러리에 구현이 되어있다.
import math
a, b = 6, 15
print(math.lcm(a, b))
print(math.gcd(a,b))
#output
#30
#3
'알고리즘 > 알고리즘(초급)' 카테고리의 다른 글
python 으로 stack 직접 구현하기 (0) | 2022.12.04 |
---|---|
투포인터 (0) | 2022.11.23 |
백트래킹을 이용한 부분 집합을 구하는 알고리즘 (0) | 2022.11.21 |
에라토스테네스의 체 (0) | 2022.11.17 |
배수와 약수 (0) | 2022.08.17 |
Comments