GCD in standard library?

Blake Garretson blakeg at metalink.net
Wed Mar 12 20:06:55 EST 2003


Unless I've been missing something for years, I don't believe there are any
functions built-in or in the standard library that find the Greatest Common
Divsor (GCD) of two integers.  This seems like a common enough math function
that I would *think* it ought to be in the math module (and gain the
advantage of being coded in C.)  I guess I'm wondering why it isn't there.
Was it to keep out code bloat?  I realize we can't include everyone's pet
function, so it's fine if it stays out.  I'm just guessing it is very common
for people to have to add this to their programs:

def gcd(x,y):
  if x % y == 0: return y
  else: return gcd(y,x%y)

I know there is a PEP for a rational number type, and I'd assume that would
have a GCD method, but I don't believe that's ready for primetime.  If
Python comes with "batteries included", where's GCD?

Just Wondering,
Blake Garretson






More information about the Python-list mailing list