c语言之辗转相除法求最大公约数
2015-01-03 22:55
246 查看
<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);
}
相关文章推荐
- 汇编语言: 根据欧几里德辗转相除法求两个正整数M、N的最大公约数R。
- java语言用辗转相除法求最大公约数
- 九度OJ 1056--最大公约数 1439--Least Common Multiple 【辗转相除法】
- 辗转相除法求最大公约数-hdu1019
- 【51nod】1011 最大公约数GCD--辗转相除法
- 辗转相除法求最大公约数
- 山东理工大学ACM平台题答案关于C语言 1181 C语言实验——最小公倍数和最大公约数
- 辗转相除法求最大公约数
- 求两个整数最大公约数的三种方法(相减法,辗转相除法,穷举法)
- 辗转相除法求最大公约数---C语言
- 求最大公约数----辗转相除法
- 求最大公约数----辗转相除法
- 辗转相除法求得最大公约数的证明
- 辗转相除法求最大公约数
- 使用辗转相除法求得最大公约数
- 利用辗转相除法来解决最大公约数问题
- 辗转相除法求最大公约数
- 辗转相除法之枚举法条件运算符求最大公约数
- 编写程序,采用辗转相除法求解两个正整数的最大公约数
- 80x86 汇编:用辗转相除法求两个数的最大公约数