Home › Programming
#### Howdy, Stranger!

##
Quick Links

# GCD of two numbers in Python

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

Titan_S
Member

in Programming

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:

python 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.