2010-06-03 8 views

Respuesta

18

Según the 3.1.2 source code online, aquí está gcd como se define en Python-3.1.2/Lib/fractions.py:

def gcd(a, b): 
    """Calculate the Greatest Common Divisor of a and b. 

    Unless b==0, the result will have the same sign as b (so that when 
    b is divided by it, the result comes out positive). 
    """ 
    while b: 
     a, b = b, a%b 
    return a 

Así que sí, es el algoritmo de Euclides, escrito en Python puro.

+0

+1. ¡Definitivo! –

+2

Si está usando IPython, puede ver el código fuente inmediatamente al escribir 'gcd ??' – endolith

+0

Es en realidad: 'import fractions', luego:' fractions.gcd ?? 'en IPython. – syntagma

Cuestiones relacionadas