Python 기본 사항중간

재귀를 사용하는 HCF

'재귀를 사용하는 HCF' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

중간

재귀를 사용하여 두 양의 정수 ab의 최대 공약수(GCD)라고도 알려진 최고 공약수(HCF)를 계산하는 함수 hcf(a, b)을 작성하세요. 유클리드 알고리즘을 사용합니다: HCF(a, b) = HCF(b, a % b), 기본 사례 HCF(a, 0) = a.

제약
  • 1 <= a, b <= 10^6

Example 1
Input
a = 12, b = 8
Output
4
Explanation

12 = 4*3, 8 = 4*2. The largest common factor is 4.

Example 2
Input
a = 54, b = 24
Output
6
Explanation

54 = 6*9, 24 = 6*4. HCF is 6.

Example 3
Input
a = 17, b = 13
Output
1
Explanation

17 and 13 are both prime and share no common factors other than 1.

Need a Hint?
세트나 힙과 같은 재귀 관련 데이터 구조를 사용하는 것을 고려해보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.