2

i have found following pseudo-code for extended euclidean algorithm

enter image description here

i implemented following algorithm

    function [x1,y1,d1]=extend_eucledian(a,b)
            if b==0
                x1=1;
                y1=0;
                d1=a;
                return;
            end

            [x1,y1,d1]=extend_eucledian(b,mod(a,b));


 x1=y1;
y1=x1-floor(a/b)*y1;
d1=d1;

        end

when i run following this program, i got following result

[x1,y1,d1]=extend_eucledian(23,20)

x1 =

     0


y1 =

     0


d1 =

     1

i guess that [x1,y1,d1] are not changing their values during the iteration, for instance , i have tried following code

    function [x1,y1,d1]=extend_eucledian(a,b)
            if b==0
                x1=1;
                y1=0;
                d1=a;
                return;
            else
                 x1=y1;
y1=x1-floor(a/b)*y1;
d1=d1;
            end

            [x1,y1,d1]=extend_eucledian(b,mod(a,b));



        end

but i got

>> [x1,y1,d1]=extend_eucledian(23,20)
Undefined function or variable 'y1'.

Error in extend_eucledian (line 8)
                 x1=y1;

how can i fix this problem? where i am making mistake?

  • In the last lines, you are reusing x1, y1, d1 and mixing "input" and "output" values. More concretely, you do x1=y1, and in the next line y1 = x1 - ..., but this last x1 is not the intended value: you overwrote it with y1 in the last line. Try asigning the result of extend_eucledian to temporary variables and then calculating final values. – dunadar Mar 30 '17 at 13:16
  • i tried bit it does not work –  Mar 30 '17 at 13:17
  • Ok, then write the corrected code. Your example seems clearly faulty to me. – dunadar Mar 30 '17 at 13:18
  • lets check my answer downstair –  Mar 30 '17 at 13:21
  • it still does not work –  Mar 30 '17 at 13:22
  • Why are you not using [`gcd`](https://www.mathworks.com/help/matlab/ref/gcd.html)? – beaker Mar 30 '17 at 14:58

2 Answers2

1

The error in the question is that x1, y1, d1 are used simultaneously as working and output variables. The code can be rewritten using a new tern [x, y d] for the output values:

function [x y d]=eucledian_pairs(a,b)
%a>0,b>0
%d=gcd(a,b)  a*x+y*b=d
[x1,y1,d1]=extend_eucledian(a,b);

x=y1;
y=x1-floor(a/b)*y1;
d=d1;    

    function [x1,y1,d1]=extend_eucledian(a,b)
        if b==0
            x1=1;
            y1=0;
            d1=a;
            return;
        end
        [x1,y1,d1]=extend_eucledian(b,mod(a,b));                
    end    
end

Executing this code gives the desired result.

>> [x y d]=eucledian_pairs(20,25)

x =

     0


y =

     1


d =

     5
dunadar
  • 1,745
  • 12
  • 30
1

The problem can be solved by introducing intermediate working variables, that will store the result of the recursive call:

function [x,y,d]=extended_euclid(a,b)
    if b==0
        x=1;
        y=0;
        d=a;
        return;
    end
    [x1,y1,d1]=extended_euclid(b,mod(a,b));  

    x=y1;
    y=x1-floor(a/b)*y1;
    d=d1;

end 

This function works as expected:

>> [x, y, d] = extended_euclid(23,20)

x =    
     7

y =    
    -8

d =    
     1


>> [x, y, d] = extended_euclid(25,20)

x =    
     1    

y =    
    -1    

d =    
     5
dunadar
  • 1,745
  • 12
  • 30