function w=modulo(a,x,n);

xb=dec2bin(x);
m=length(xb);

XC=[];
for i=1:m;
    XC=[XC xb(m+1-i)];
end;

S=[];
for i=1:m;
    s=mod(a,n);
    a=power(s,2);
    S=[S s];
end;



if mod(m,2)>0
    S=[S 1];
    XC=[XC '0'];
end;

C=[];
for i=1:2:m
    C=[C mod(power(S(i), str2num(XC(i)))*power(S(i+1), str2num(XC(i+1))), n)];
end;



while length(C)>1 
    C=mnozi_dva(C, n);
end;

w=mod(C(1),n);
    
