2010-06-03 8 views

Répondre

18

Selon the 3.1.2 source code online, voici gcd tel que défini dans 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 

Alors oui, il est l'algorithme d'Euclide, écrit en Python pur.

+0

+1. Définitive! –

+2

Si vous utilisez IPython, vous pouvez voir le code source immédiatement en tapant 'gcd ??' – endolith

+0

C'est en fait: 'import fractions', alors:' fractions.gcd ?? 'dans IPython. – syntagma

Questions connexes