Easy
Find the Greatest Common Divisor
- Time limit
- 1 s
- Memory limit
- 128 MB
- Estimated
- 15 min
- GCD and Divisibility
Description
Given two non-negative integers a and b, find and return their greatest common divisor (GCD).
The greatest common divisor is the largest positive integer that divides both numbers without leaving a remainder.
For example, the GCD of 24 and 36 is 12 because 12 is the largest integer that divides both numbers exactly.
You may use the Euclidean algorithm or another correct approach.
If one number is zero, the GCD is the other number. The input guarantees that both numbers will not be zero simultaneously.
Implement the function gcd(a, b) that returns the greatest common divisor.
Constraints
0 <= a <= 1000000000 0 <= b <= 1000000000 At least one of a and b is greater than zero.
Sample cases
3Sample 1
Input
a = 24 b = 36
Expected output
12
Sample 2
Input
a = 17 b = 13
Expected output
1
Sample 3
Skills and languages
Concepts
- Euclidean Algorithm
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer