最大公约数:(函数)
function n = zuidagongyueshu(a,b)
if(a>b)
tem = a;
b = a;
a = tmp;
end
for i=1:a
c = rem(b,a);
if(c == 0)
n = a;
break;
end
b = a;
a = c;
end
测试:
clc;
clear;
a = 8;
b = 22;
disp('最大公约数为:');
zuidagongyueshu(a,b)
最小公倍数:(函数)
function n = zuixiaogongbeishu(a,b)
if(a>b)
tem = a;
a = b;
b = tem;
end
n = 1;
for i = 1:a
if(rem(a,i)==0&&rem(b,i)==0)
n = n * i;
a = a/i;
b = b/i;
end
end
n = n*a*b;
end
测试:
clc;
clear;
a = 12;
b = 9;
disp('最小公倍数是:');
n = zuixiaogongbeishu(a,b)