i have implemented binary eucledian algorithm in matlab, here is code
function d=EuclidBinary(a,b)
if a==0
d=b;
return ;
elseif b==0
d=a;
return;
elseif mod(a,2)==0 && mod(b,2)==0
d=2*EuclidBinary(a/2,b/2);
elseif mod(a,2)==0 && mod(b,2)==1
d=EuclidBinary(a/2,b);
elseif mod(a,2)==1 && mod(b,2)==0
d=EuclidBinary(a,b/2);
else
d=EuclidBinary(fix(abs(a-b)/2),b);
end
then i have entered following data
a=27;
b=36;
d=EuclidBinary(a,b)
d =
9
it works fine, but when i have changed to the following data
a=36;
b=28;
d=EuclidBinary(a,b)
Out of memory. The likely cause is an infinite recursion within the program.
Error in EuclidBinary (line 16)
d=EuclidBinary(fix(abs(a-b)/2),b);
i have started debugging and if i follow instructions, then i will have
d=2*EuclidBinary(a/2,b/2); for first call (a=18,b=14) for the second the same d=2*EuclidBinary(a/2,b/2); (a=9,b=7)
for the third case we have
a=1 b=7
and in generally program repeats those values for infinite times,i use following pseudo code
please help me to fix this