I am trying to find the greatest common divisor (GCD) of two numbers in Python from here. I found a few different algorithms online, but I am not sure which one is the most efficient.
Here is the code I have so far:
Expand|Select|Wrap|Line Numbers
- def gcd(x, y):
- while y:
- x, y = y, x % y
- return x
- print(gcd(10, 15))
In this case, the output of the code is 5, which is the GCD of 10 and 15.
I am wondering if there is a more efficient way to find the GCD of two numbers in Python.