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 | 29 | 30 | 31 |
Tags
- 백준
- NLP
- 알고리즘
- docker
- 네이버AItech
- wandb
- pep8
- NaverAItech
- GIT
- GCP
- github
- vscode
- Matplotlib
- FastAPI
- 완전탐색
- PytorchLightning
- 코딩테스트
- python
- autoencoder
- GitHub Action
- torchserve
- 프로그래머스
- FDS
- leetcode
- datascience
- Kaggle
- DeepLearning
- Kubernetes
- pytorch
- rnn
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
유클리드 호제법 - 위키백과, 우리 모두의 백과사전
위키백과, 우리 모두의 백과사전. 유클리드 호제법(-互除法, Euclidean algorithm) 또는 유클리드 알고리즘은 2개의 자연수 또는 정식(整式)의 최대공약수를 구하는 알고리즘의 하나이다. 호제법이란
ko.wikipedia.org
* 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