Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

GCD of two numbers in Python

Hi everyone,

I am trying to implement the GCD of two numbers in Python. I have 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:


def gcd(x, y):

while y:

x, y = y, x % y

return x

print(gcd(10, 15))

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. This is because 5 is the GCD of 10 and 15.

I would appreciate it if someone could help me to understand which of these algorithms is the most efficient and why.

Sign In or Register to comment.