Ethiopian multiplication: Difference between revisions

→‎{{header|MATLAB}}: Optimized and commented the code
(→‎{{header|MATLAB}}: Optimized and commented the code)
Line 986:
=={{header|MATLAB}}==
 
In order to run this program you need to have MATLAB already installed. Save the program as Ethiopian.m, then into the command prompt type Ethiopian(a,b) where a & b are the two numbers you require to multiply. To see how this function operates, remove the semi-colons.
 
<lang matlab>function Aanswer = Ethiopian(amultiplicand,bmultiplier)
 
j=1;
%Generate columns
i=1;
while amultiplicand(iend)>1
a multiplicand(iend+1,1) = floor(a multiplicand(iend)/2 );
b multiplier(iend+1,1) =b multiplier(iend)*2;
i=i+1;
end
N=length(a);
for j=1:N
if a(j)/2==floor(a(j)/2)
b(j)=0;
end
end
%Strike out appropriate rows
A=sum(b);</lang>
multiplier( find( mod(multiplicand,2)==0 ) ) = [];
%Generate answer
answer = sum(multiplier);
A=sum(b);end</lang>
 
=={{header|MMIX}}==
Anonymous user