Hi everyone,
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:
This code uses Euclid's algorithm to find the GCD of two numbers. It works by repeatedly subtracting the smaller number from the larger number until the remainder is 0. The remainder is then the GCD of the two numbers.
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.
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:
Code:
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.