2010-06-03 13 views

risposta

18

Secondo the 3.1.2 source code online, ecco gcd come definito nella 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 

Quindi sì, è l'algoritmo di Euclide, scritto in puro Python.

+0

+1. Definitivo! –

+2

Se stai usando IPython, puoi vedere immediatamente il codice sorgente digitando 'gcd ??' – endolith

+0

In realtà: 'import fractions', quindi:' fractions.gcd ?? 'in IPython. – syntagma

Problemi correlati