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
- C语言 辗转相除法求最大公约数
- 辗转相除法求最大公约数---C语言
- c语言 辗转相除法求最大公约数
- c语言之辗转相除法求最大公约数
- 辗转相除法求最大公约数(C语言)
- C语言--辗转相除法求最大公约数和最小公倍数
- C语言--辗转相除法求最大公约数和最小公倍数
- C语言辗转相除法(欧几里德算法)求最大公约数
- C语言辗转相除法求最大公约数(欧几里得算法)
- C语言_辗转相除法求最大公约数
- C语言第七篇:辗转相除法求最大公约数
- 【c语言】用辗转相除法求最大公约数和最小公倍数
- c语言用辗转相除法实现求最大公约数
- C语言 求两个数的最大公约数 (算法)--辗转相减法、辗转相除法
- 辗转相除法求最大公约数
- 辗转相除法求最大公约数
- 辗转相除法求最大公约数
- 辗转相除法求最大公约数
- BCB中的TMemo(通过ScrollBars属性可以激活TMemo内置的滚动条)
- Mysql创建用户与授权
- ARM TCM简介
- [Leetcode] #32 Longest Valid Parentheses
- 九度OJ 1002 Grading
- c语言之辗转相除法求最大公约数
- 一位前端开发者的计算机视觉学习之路——专访《Chrome扩展及应用开发》作者李喆
- 自学日记插曲:Linux的学习(day1)
- 专访《硅谷百年史》作者:人们不想以旧的方式生活,却不知道自己想要什么
- 哈佛顶级创业家课程教授Thomas Eisenmann:颠覆创始人是创业的一部分
- mongodb http interface
- 后缀自动机(多个穿的最长公共子串)spoj1812
- javascript ++\--
- CALayerDelegate的实现(IOS)