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

来源:互联网 发布:室内设计好的软件 编辑:程序博客网 时间:2024/06/04 01:34

<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
原创粉丝点击