Nathan Harmston wrote: > Hi, > > I m playing around with extended euclids algorithm from Knuth. I m > trying to build a function with a function inside it. > > def exteuclid(m,n): > a,a1,b,b1,c,d = 0,1,1,0,m,n > def euclid(c,d): > q = c /d > r = c % d > if r == 0: > print a,b > return d > else: > print a1,a,b1,b,c,d,q,r > t = b1 > b = t - q * b > a = t - q * a > c,d,a1,b1 = d,r,a,b > return euclid(c,d) > return euclid(c,d) > > Unfortunately this doesnt work since a,a1,b,b1 arent declared in the > function. Is there a way to make these variables accessible to the > euclid function. Or is there a better way to design this function? > > Many Thanks in advance, > > Nathan
That last return statement does not match indentation of another block. But this is probably what you mean: def exteuclid(m,n): x = 0,1,1,0,m,n def euclid(c,d,x=x): a,a1,b,b1,c,d = x q = c /d r = c % d if r == 0: print a,b return d else: print a1,a,b1,b,c,d,q,r t = b1 b = t - q * b a = t - q * a c,d,a1,b1 = d,r,a,b return euclid(c,d) return euclid(c,d) James -- http://mail.python.org/mailman/listinfo/python-list