c语言之辗转相除法求最大公约数

<pre name="code" class="html">

</pre><pre name="code" class="html"></pre>辗转相除法</h1><p><span style="font-size:14px">      <span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">辗转相除法,又名</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/1241014.htm" style="text-decoration:none; color:rgb(19,110,194); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">欧几里德算法</a><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">(Euclidean algorithm)乃求两个</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/464125.htm" style="text-decoration:none; color:rgb(19,110,194); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">正整数</a><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">之</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/1103370.htm" style="text-decoration:none; color:rgb(19,110,194); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">最大公因子</a><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">的算法。它是已知最古老的算法, 其可追溯至3000年前。</span></span></p><p><span style="font-size:14px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:14px; line-height:24px; text-indent:28px">       其过程为:已知a,b两个数,其中a大于b。则a对b求于a%b,得到余数r。因为余数r小于b,则把b,r中较大的数b赋值给a,r赋值给b,再b对r求余数,以此往复,直到余数为零。</span></span></p><p style="text-indent:28px"><span style="font-family:arial,宋体,sans-serif; color:#333333"><span style="font-size:14px; line-height:24px">在下面辗转相除法函数创建为:void Euclid(int a, int b);</span></span></p><h1><span style="line-height:24px; color:rgb(51,51,51); font-family:arial,宋体,sans-serif; font-size:24px">原理</span></h1><p style="text-indent:28px"><span style="font-family:arial,宋体,sans-serif; color:#333333"><span style="font-size:14px; line-height:24px"></span></span></p><div class="para" style="color:rgb(51,51,51); margin:15px 0px 5px; text-indent:2em; line-height:24px; font-family:arial,宋体,sans-serif; font-size:14px">设两数为a、b(b<a),用gcd(a,b)表示a,b的最大公约数,r=a mod b 为a除以b以后的余数,k为a除以b的商,即a÷b=k.......r。辗转相除法即是要证明gcd(a,b)=gcd(b,r)。</div><div class="para" style="color:rgb(51,51,51); margin:15px 0px 5px; text-indent:2em; line-height:24px; font-family:arial,宋体,sans-serif; font-size:14px">第一步:令c=gcd(a,b),则设a=mc,b=nc</div><div class="para" style="color:rgb(51,51,51); margin:15px 0px 5px; text-indent:2em; line-height:24px; font-family:arial,宋体,sans-serif; font-size:14px">第二步:根据前提可知r =a-kb=mc-knc=(m-kn)c</div><div class="para" style="color:rgb(51,51,51); margin:15px 0px 5px; text-indent:2em; line-height:24px; font-family:arial,宋体,sans-serif; font-size:14px">第三步:根据第二步结果可知c也是r的因数</div><div class="para" style="color:rgb(51,51,51); margin:15px 0px 5px; text-indent:2em; line-height:24px; font-family:arial,宋体,sans-serif; font-size:14px">第四步:可以断定m-kn与n互质【否则,可设m-kn=xd,n=yd,(d>1),则m=kn+xd=kyd+xd=(ky+x)d,则a=mc=(ky+x)dc,b=nc=ycd,故a与b最大公约数成为cd,而非c,与前面结论矛盾】</div><div class="para" style="color:rgb(51,51,51); margin:15px 0px 5px; text-indent:2em; line-height:24px; font-family:arial,宋体,sans-serif; font-size:14px">(互质:公约数只有1的两个数,称为互质数)</div><div class="para" style="color:rgb(51,51,51); margin:15px 0px 5px; text-indent:2em; line-height:24px; font-family:arial,宋体,sans-serif; font-size:14px">从而可知gcd(b,r)=c,继而gcd(a,b)=gcd(b,r)。</div><div class="para" style="color:rgb(51,51,51); margin:15px 0px 5px; text-indent:2em; line-height:24px; font-family:arial,宋体,sans-serif; font-size:14px">证毕。</div><h1>算法代码如下:</h1><div></div><pre name="code" class="objc" style="font-size: 24px; font-weight: bold;">void euclid(int a,int b)
{
    int temp;
    
    if(a < b)
    {
        temp = a;
        a = b;
        b = temp;
    }
    
    while(temp=a%b)
    {
        a = b;
        b = temp;
    }
    
    printf("a和b的最大公约数为:%d",b);
    
}

 
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值