求一个关于最大公因子的matlab代码
求一个matlab程序能满足以下两个条件:1、已知有两个整数a和b,要能求出他们的最大公因子c
我们知道最大公因子c可以写成c=p*a+q*b的形式,p,q也是整数,条件二就是:
2、要能求出p和q
求大神帮忙解答,谢谢!
1. MATLAB 有自带的函数gcd
2. 查看函数说明 doc gcd
3. 查看函数代码 edit gcd
4. 调用格式 = gcd(a,b)
5. 案例: = gcd(126,66)
结果: c=6,p=-1,q=2.
附上程序源代码,仅供参考。
<FONT color=black size=3>function = gcd(a,b)
%GCD Greatest common divisor.
% G = GCD(A,B) is the greatest common divisor of corresponding elements
% of A and B. The arrays A and B must contain integer values and must be
% the same size (or either can be scalar). GCD(0,0) is 0 by convention;
% all other GCDs are positive integers.
%
% = GCD(A,B) also returns C and D so that G = A.*C + B.*D.
% These are useful for solving Diophantine equations and computing
% Hermite transformations.
%
% Class support for inputs A,B:
% float: double, single
% integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64
%
% See also LCM.
% References:
% Knuth, Donald, The Art of Computer Programming, Vol. 2, Addison-Wesley:
% Reading MA, 1973. Section 4.5.2, Algorithms A and X.
%
% Thanks to John Gilbert for the original version
% Copyright 1984-2012 The MathWorks, Inc.
% $$$$Revision: 5.14.4.9 $$$$ $$$$Date: 2012/10/15 20:09:56 $$$$
if ~isequal(size(a),size(b)) && ~isscalar(a) && ~isscalar(b)
error(message('MATLAB:gcd:InputSizeMismatch'))
end
if ~isscalar(a)
siz = size(a);
else
siz = size(b);
end
a = a(:); b = b(:);
if ~isreal(a) || ~isequal(round(a),a) || any(isinf(a)) || ...
~isreal(b) || ~isequal(round(b),b) || any(isinf(b))
error(message('MATLAB:gcd:NonIntInputs'))
end
if isinteger(a)
if ~(strcmp(class(a),class(b)) || (isa(b,'double') && isscalar(b)))
error(message('MATLAB:gcd:mixedIntegerTypes'))
end
classin = class(a);
if isa(b,'double') && (b > intmax(classin) || b < intmin(classin))
error(message('MATLAB:gcd:outOfRange'));
end
inttype = true;
elseif isinteger(b)
if ~(isa(a,'double') && isscalar(a))
error(message('MATLAB:gcd:mixedIntegerTypes'))
end
classin = class(b);
if a > intmax(classin) || a < intmin(classin)
error(message('MATLAB:gcd:outOfRange'));
end
inttype = true;
else
classin = superiorfloat(a,b);
largestFlint = flintmax(classin);
if any(abs(a) > largestFlint) || any(abs(b) > largestFlint)
warning(message('MATLAB:gcd:largestFlint'));
end
inttype = false;
end
if nargout <= 1
% intmin in signed integers requires special handling
iminIndex = [];
if inttype
imin = intmin(classin);
if imin < 0
iminIndex = xor(a == imin, b == imin);
end
end
u = max(abs(a),abs(b));
v = min(abs(a),abs(b));
u(iminIndex) = u(iminIndex)/2;
vnz = v>0;
while any(vnz)
t = rem(u,v);
u(vnz) = v(vnz);
v(vnz) = t(vnz);
vnz = v>0;
end
g = reshape(u,siz);
else
if inttype
if intmin(classin) == 0 % unsigned integers not supported
error(message('MATLAB:gcd:unsupportedType'));
end
end
len = prod(siz);
if issparse(a) || issparse(b)
u = spalloc(len,3,nnz(a)+len);
else
u = zeros(len,3,classin);
end
u(:,1) = 1;
u(:,3) = a;
if issparse(b)
v = spalloc(len,3,nnz(b)+len);
else
v = zeros(len,3,classin);
end
v(:,2) = 1;
v(:,3) = b;
vnz = v(:,3)~=0;
while any(vnz)
if inttype
q = idivide(u(:,3),v(:,3),'fix');
else
q = fix( u(:,3)./v(:,3));
end
t = u - bsxfun(@times, v, q);
u(vnz,:) = v(vnz,:);
v(vnz,:) = t(vnz,:);
vnz = v(:,3)~=0;
end
g = reshape(u(:,3),siz);
c = reshape(u(:,1),siz).*sign(g);
d = reshape(u(:,2),siz).*sign(g);
g = abs(g);
% correct overflow conditions in signed integers
if inttype
overflow1 = reshape(a == intmin(classin) & b == -1, siz);
overflow2 = reshape(a == -1 & b == intmin(classin), siz);
g(overflow1 | overflow2) = 1;
c(overflow1) = 0;
d(overflow1) = -1;
c(overflow2) = -1;
d(overflow2) = 0;
end
end
</FONT>
同学,要学会使用Matlab中的帮助文档!!!
森之张卫东 发表于 2015-10-23 18:50 static/image/common/back.gif
同学,要学会使用Matlab中的帮助文档!!!
我懂得打开这个,但看不懂这个代码,满足我的条件不需要这么复杂吧
士心之约 发表于 2015-10-23 18:10 static/image/common/back.gif
1. MATLAB 有自带的函数gcd
2. 查看函数说明 doc gcd
我懂得打开这个,但看不懂这个代码,满足我的条件不需要这么复杂吧
我是来混禁言的
没有我想要的答案,帮助文档谁不懂打开,我看不懂matlab的帮助文档,看不出其中编程的思路,才在此求助。就贴个matlab文档在这,不值300金币。
function GCD=Euclid(m,n)
%欧几里德算法
r=mod(m,n);
while r~=0
r=mod(m,n);
m=n;
n=r;
end
GCD=m;
>> m=319;n=377;Euclid(m,n)
ans =
29
页:
[1]