数学建模社区-数学中国

标题: 求一个关于最大公因子的matlab代码 [打印本页]

作者: 无相天    时间: 2015-10-23 15:50
标题: 求一个关于最大公因子的matlab代码
    求一个matlab程序能满足以下两个条件:
    1、已知有两个整数a和b,要能求出他们的最大公因子c
    我们知道最大公因子c可以写成c=p*a+q*b的形式,p,q也是整数,条件二就是:
    2、要能求出p和q
     求大神帮忙解答,谢谢!                                                   


作者: 士心之约    时间: 2015-10-23 18:10
1. MATLAB 有自带的函数gcd

2. 查看函数说明 doc gcd

3. 查看函数代码 edit gcd

4. 调用格式
[c,p,q] = gcd(a,b)

5. 案例: [c,p,q] = gcd(126,66)
    结果: c=6,p=-1,q=2.

附上程序源代码,仅供参考。
  1. <FONT color=black size=3>function [g,c,d] = gcd(a,b)
  2. %GCD    Greatest common divisor.
  3. %   G = GCD(A,B) is the greatest common divisor of corresponding elements
  4. %   of A and B.  The arrays A and B must contain integer values and must be
  5. %   the same size (or either can be scalar). GCD(0,0) is 0 by convention;
  6. %   all other GCDs are positive integers.
  7. %
  8. %   [G,C,D] = GCD(A,B) also returns C and D so that G = A.*C + B.*D.
  9. %   These are useful for solving Diophantine equations and computing
  10. %   Hermite transformations.
  11. %
  12. %   Class support for inputs A,B:
  13. %      float: double, single
  14. %      integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64
  15. %
  16. %   See also LCM.

  17. %   References:
  18. %   Knuth, Donald, The Art of Computer Programming, Vol. 2, Addison-Wesley:
  19. %      Reading MA, 1973. Section 4.5.2, Algorithms A and X.

  20. %
  21. %   Thanks to John Gilbert for the original version
  22. %   Copyright 1984-2012 The MathWorks, Inc.
  23. %   $$$$Revision: 5.14.4.9 $$$$  $$$$Date: 2012/10/15 20:09:56 $$$$

  24. if ~isequal(size(a),size(b)) && ~isscalar(a) && ~isscalar(b)
  25.     error(message('MATLAB:gcd:InputSizeMismatch'))
  26. end

  27. if ~isscalar(a)
  28.     siz = size(a);
  29. else
  30.     siz = size(b);
  31. end
  32. a = a(:); b = b(:);

  33. if ~isreal(a) || ~isequal(round(a),a) || any(isinf(a)) || ...
  34.         ~isreal(b) || ~isequal(round(b),b) || any(isinf(b))
  35.     error(message('MATLAB:gcd:NonIntInputs'))
  36. end

  37. if isinteger(a)
  38.     if ~(strcmp(class(a),class(b)) || (isa(b,'double') && isscalar(b)))
  39.         error(message('MATLAB:gcd:mixedIntegerTypes'))
  40.     end
  41.     classin = class(a);
  42.     if isa(b,'double') && (b > intmax(classin) || b < intmin(classin))
  43.         error(message('MATLAB:gcd:outOfRange'));
  44.     end
  45.     inttype = true;
  46. elseif isinteger(b)
  47.     if ~(isa(a,'double') && isscalar(a))
  48.         error(message('MATLAB:gcd:mixedIntegerTypes'))
  49.     end
  50.     classin = class(b);
  51.     if a > intmax(classin) || a < intmin(classin)
  52.         error(message('MATLAB:gcd:outOfRange'));
  53.     end
  54.     inttype = true;
  55. else
  56.     classin = superiorfloat(a,b);
  57.     largestFlint = flintmax(classin);
  58.     if any(abs(a) > largestFlint) || any(abs(b) > largestFlint)
  59.         warning(message('MATLAB:gcd:largestFlint'));
  60.     end
  61.     inttype = false;
  62. end

  63. if nargout <= 1
  64.     % intmin in signed integers requires special handling
  65.     iminIndex = [];
  66.     if inttype
  67.         imin = intmin(classin);
  68.         if imin < 0
  69.             iminIndex = xor(a == imin, b == imin);
  70.         end
  71.     end
  72.     u = max(abs(a),abs(b));
  73.     v = min(abs(a),abs(b));
  74.     u(iminIndex) = u(iminIndex)/2;
  75.     vnz = v>0;
  76.     while any(vnz)
  77.         t = rem(u,v);
  78.         u(vnz) = v(vnz);
  79.         v(vnz) = t(vnz);
  80.         vnz = v>0;
  81.     end
  82.     g = reshape(u,siz);
  83. else
  84.     if inttype
  85.         if intmin(classin) == 0    % unsigned integers not supported
  86.             error(message('MATLAB:gcd:unsupportedType'));
  87.         end
  88.     end
  89.     len = prod(siz);
  90.     if issparse(a) || issparse(b)
  91.         u = spalloc(len,3,nnz(a)+len);
  92.     else
  93.         u = zeros(len,3,classin);
  94.     end
  95.     u(:,1) = 1;
  96.     u(:,3) = a;
  97.     if issparse(b)
  98.         v = spalloc(len,3,nnz(b)+len);
  99.     else
  100.         v = zeros(len,3,classin);
  101.     end
  102.     v(:,2) = 1;
  103.     v(:,3) = b;
  104.     vnz = v(:,3)~=0;
  105.     while any(vnz)
  106.         if inttype
  107.             q = idivide(u(:,3),v(:,3),'fix');
  108.         else
  109.             q = fix( u(:,3)./v(:,3));
  110.         end
  111.         t = u - bsxfun(@times, v, q);
  112.         u(vnz,:) = v(vnz,:);
  113.         v(vnz,:) = t(vnz,:);
  114.         vnz = v(:,3)~=0;
  115.     end
  116.    
  117.    
  118.     g = reshape(u(:,3),siz);
  119.     c = reshape(u(:,1),siz).*sign(g);
  120.     d = reshape(u(:,2),siz).*sign(g);
  121.     g = abs(g);
  122.     % correct overflow conditions in signed integers
  123.     if inttype
  124.         overflow1 = reshape(a == intmin(classin) & b == -1, siz);
  125.         overflow2 = reshape(a == -1 & b == intmin(classin), siz);
  126.         g(overflow1 | overflow2) = 1;
  127.         c(overflow1) = 0;
  128.         d(overflow1) = -1;
  129.         c(overflow2) = -1;
  130.         d(overflow2) = 0;
  131.     end
  132. end
  133. </FONT>
复制代码


作者: 森之张卫东    时间: 2015-10-23 18:50

        同学,要学会使用Matlab中的帮助文档!!!

help文档.PNG (172.74 KB, 下载次数: 490)

help文档.PNG


作者: 无相天    时间: 2015-10-23 23:46
森之张卫东 发表于 2015-10-23 18:50
同学,要学会使用Matlab中的帮助文档!!!

我懂得打开这个,但看不懂这个代码,满足我的条件不需要这么复杂吧

作者: 无相天    时间: 2015-10-23 23:46
士心之约 发表于 2015-10-23 18:10
1. MATLAB 有自带的函数gcd

2. 查看函数说明 doc gcd

我懂得打开这个,但看不懂这个代码,满足我的条件不需要这么复杂吧

作者: 林威123    时间: 2015-10-27 22:23
我是来混禁言的

作者: 无相天    时间: 2015-11-14 14:11
没有我想要的答案,帮助文档谁不懂打开,我看不懂matlab的帮助文档,看不出其中编程的思路,才在此求助。就贴个matlab文档在这,不值300金币。

作者: hzlhm    时间: 2015-11-17 20:46
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





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5