randomly generate M pairs of complex numbers from 1 to N, find gcd.

44 Views Asked by At

I want to write a script to generate statistics on gcd's and number of steps required to find them by the Euclidean algorithm, using M randomly generated pairs $a+bi$ between 1 and N. And plot them. Can someone help me improve this and get it to work?

gcdcount=zeros(1,1);
stepcount=zeros(1,1);
M=100
N=50
for j=1:M    
a1=input('Real part of first complex number = ');
a2=input('imaginary part of first complex number = ');
b1=input('Real part of second complex number = ');
b2=input('imaginary part of second complex number = ');
z1=complex(a1,a2);
z2=complex(b1,b2);
count=0;
if(abs(z1)<abs(z2))  % switch z1 and z2, if necessary, so that b<a
    c=z1; % hang onto the value of a
    z1=z2; % even while replacing a with b
    z2=c; % now replace b with a
end
count=0; % initialize counter
while(abs(z2)>0)
    u=z1;
    v=z2;
    z1=z2;

    q=(u/v);
    q1=real(q);
    q2=imag(q);
    if (q1-floor(q1)<=0.5) 
        q1=floor(q1);
    else
        q1=1+floor(q1);
    end

        if (q2-floor(q2)<=0.5) 
        q2=floor(q2);
        else
            q2=1+floor(q2);
        end

    q=complex(q1,q2);
    r=u-v*q;
    z2=r;
    q
    r
    count=count+1;
end
end
z1
count
subplot(2,1,1)
plot(gcdcount/M)
title('Distribution of gcds')
subplot(2,1,2)
plot(stepcount/M)
title('Distribution of algorithm steps')
subplot(111) % means the figure window returns to normal single-graph behavior

But when I ran it, it wouldn't randomly generate 100 pairs of complex numbers and find their gcds and give me a plot the statistics, instead it asked me to input every complex numbers I want