Skip to main content
just formatted a and b for better readability
Source Link

If you want to know whether there are more efficient algorithms than Euclid's, this is probably the wrong place to ask. Even Wikipedia would be a better starting point. The answer is yes but they are far more complicated. Intuitively I'd ignore Stein's algorithm (on that page as "Binary GCD algorithm") for Python because it relies on low level tricks like bit shifts that Python really doesn't excel at. Euclid's algorithm is probably fine.

In terms of your implementation of the Euclidean algorithm

  • You don't need to manually check which of aa and bb is greater. If they're the wrong way round, the first % operation will swap them for you.
  • You should have your function return a value rather than print a value. Print can be called from outside.
  • You should consider how you'll handle negative numbers. As written this will give a negative GCD for (-5 and 10). It will crash for (5 and -10). The right answer is probably 5.

If you want to know whether there are more efficient algorithms than Euclid's, this is probably the wrong place to ask. Even Wikipedia would be a better starting point. The answer is yes but they are far more complicated. Intuitively I'd ignore Stein's algorithm (on that page as "Binary GCD algorithm") for Python because it relies on low level tricks like bit shifts that Python really doesn't excel at. Euclid's algorithm is probably fine.

In terms of your implementation of the Euclidean algorithm

  • You don't need to manually check which of a and b is greater. If they're the wrong way round, the first % operation will swap them for you.
  • You should have your function return a value rather than print a value. Print can be called from outside.
  • You should consider how you'll handle negative numbers. As written this will give a negative GCD for (-5 and 10). It will crash for (5 and -10). The right answer is probably 5.

If you want to know whether there are more efficient algorithms than Euclid's, this is probably the wrong place to ask. Even Wikipedia would be a better starting point. The answer is yes but they are far more complicated. Intuitively I'd ignore Stein's algorithm (on that page as "Binary GCD algorithm") for Python because it relies on low level tricks like bit shifts that Python really doesn't excel at. Euclid's algorithm is probably fine.

In terms of your implementation of the Euclidean algorithm

  • You don't need to manually check which of a and b is greater. If they're the wrong way round, the first % operation will swap them for you.
  • You should have your function return a value rather than print a value. Print can be called from outside.
  • You should consider how you'll handle negative numbers. As written this will give a negative GCD for (-5 and 10). It will crash for (5 and -10). The right answer is probably 5.
Source Link
Josiah
  • 6.1k
  • 2
  • 18
  • 39

If you want to know whether there are more efficient algorithms than Euclid's, this is probably the wrong place to ask. Even Wikipedia would be a better starting point. The answer is yes but they are far more complicated. Intuitively I'd ignore Stein's algorithm (on that page as "Binary GCD algorithm") for Python because it relies on low level tricks like bit shifts that Python really doesn't excel at. Euclid's algorithm is probably fine.

In terms of your implementation of the Euclidean algorithm

  • You don't need to manually check which of a and b is greater. If they're the wrong way round, the first % operation will swap them for you.
  • You should have your function return a value rather than print a value. Print can be called from outside.
  • You should consider how you'll handle negative numbers. As written this will give a negative GCD for (-5 and 10). It will crash for (5 and -10). The right answer is probably 5.