最大公约数算法,又称欧几里德算法,至今已有几千年的历史了。在我们开始学习C语言的时候最常用的算法就是辗转相除法,其实在linux内核中,内核也是使用这样的方法实现两数最大公约数的计算。

两个整数的最大公约数是能够同时整除它们的最大的正整数。辗转相除法基于如下原理:两个整数的最大公约数等于其中较小的数和两数的相除余数的最大公约数。

例如,252和105的最大公约数是21(252
= 21 × 12;105 = 21 × 5);

算法原理:

设两数为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)。
第一步:令c=gcd(a,b),则设a=mc,b=nc
第二步:根据前提可知r =a-kb=mc-knc=(m-kn)c
第三步:根据第二步结果可知c也是r的因数
第四步:可以断定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,与前面结论矛盾】
从而可知gcd(b,r)=c,继而gcd(a,b)=gcd(b,r)。

http://home.ebrun.com/u-1120007.html

http://home.ebrun.com/u-1120008.html

http://home.ebrun.com/u-1120009.html

http://home.ebrun.com/u-1120011.html

http://home.ebrun.com/u-1120015.html

http://home.ebrun.com/u-1120013.html

http://home.ebrun.com/u-1120021.html

http://home.ebrun.com/u-1120019.html

http://home.ebrun.com/u-1120016.html

http://home.ebrun.com/u-1120024.html

http://home.ebrun.com/u-1120012.html

http://home.ebrun.com/u-1120023.html

http://home.ebrun.com/u-1120025.html

http://home.ebrun.com/u-1120022.html

http://home.ebrun.com/u-1120018.html

http://home.ebrun.com/u-1120020.html

http://home.ebrun.com/u-1120014.html

http://home.ebrun.com/u-1120010.html

http://home.ebrun.com/u-1120017.html

http://home.ebrun.com/u-1120031.html

http://home.ebrun.com/u-1120032.html

http://home.ebrun.com/u-1120033.html

http://home.ebrun.com/u-1120034.html

http://home.ebrun.com/u-1120035.html

http://home.ebrun.com/u-1120036.html

http://home.ebrun.com/u-1120038.html

http://home.ebrun.com/u-1120037.html

http://home.ebrun.com/u-1120039.html

http://home.ebrun.com/u-1120041.html

http://home.ebrun.com/u-1120043.html

http://home.ebrun.com/u-1120053.html

http://home.ebrun.com/u-1120051.html

http://home.ebrun.com/u-1120047.html

http://home.ebrun.com/u-1120044.html

http://home.ebrun.com/u-1120045.html

http://home.ebrun.com/u-1120042.html

http://home.ebrun.com/u-1120050.html

http://home.ebrun.com/u-1120054.html

http://home.ebrun.com/u-1120048.html

http://home.ebrun.com/u-1120052.html

http://home.ebrun.com/u-1120061.html

http://home.ebrun.com/u-1120056.html

http://home.ebrun.com/u-1120062.html

http://home.ebrun.com/u-1120040.html

http://home.ebrun.com/u-1120057.html

http://home.ebrun.com/u-1120055.html

http://home.ebrun.com/u-1120059.html

http://home.ebrun.com/u-1120064.html

http://home.ebrun.com/u-1120060.html

C语言之linux内核实现最大公约数算法的更多相关文章

  1. C语言之linux内核实现平方根计算算法

    关于平方根的计算,在linux内核中也有实现,就像math.h数学库里的sqrt这个函数一样. 平方根的公式定义: 如果一个非负数x的平方等于a,即    ,    ,那么这个非负数x叫做a的算术平方 ...

  2. Linux内核-内存回收逻辑和算法(LRU)

    Linux内核内存回收逻辑和算法(LRU) LRU 链表 在 Linux 中,操作系统对 LRU 的实现主要是基于一对双向链表:active 链表和 inactive 链表,这两个链表是 Linux ...

  3. C语言之linux内核实现位数高低位互换

    linux内核实在是博大精深,有很多优秀的算法,我之前在工作中就遇到过位数高低位交换的问题,那时候对于C语言还不是很熟练,想了很久才写出来.最近在看内核的时候看到有内核的工程师实现了这样的算法,和我之 ...

  4. C语言在linux内核中do while(0)妙用之法

    为什么说do while(0) 妙?因为它的确就是妙,而且在linux内核中实现是相当的妙,我们来看看内核中的相关代码: #define db_error(fmt, ...) \ do { \ fpr ...

  5. C语言之linux内核可变参实现printf,sprintf

    昨天,我发表了一篇用可变参实现的fprintf函数,其实说实话还不完全是可变参实现的,因为用到了FILE * 这样的指针,需要包含stdio.h这个头文件才能实现这个函数,今天我们就来看看,如何抛弃s ...

  6. C语言之linux内核--BCD码转二进制与二进制转BCD码(笔试经典)

    在分析代码之前,我们先来了解一下,BCD码和二进制到底区别在哪? 学习过计算机原理的和数字电子技术这两门课的都会知道这两个到底是什么含义,也有的同学学过了,考过了,过了一段时间又忘记了,今天,我们通过 ...

  7. go例子(一) 使用go语言实现linux内核中的list_head

    package list 代码 package list import ( "fmt" ) // 数据接口 type ElemType interface{} // 节点 type ...

  8. linux 内核 内存管理 slub算法 (一) 原理

    http://blog.csdn.net/lukuen/article/details/6935068

  9. (笔记)Linux内核中内存相关的操作函数

    linux内核中内存相关的操作函数 1.kmalloc()/kfree() static __always_inline void *kmalloc(size_t size, gfp_t flags) ...

随机推荐

  1. springMVC源码分析--拦截器HandlerExecutionChain(三)

    上一篇博客springMVC源码分析--HandlerInterceptor拦截器调用过程(二)中我们介绍了HandlerInterceptor的执行调用地方,最终HandlerInterceptor ...

  2. windows下Eclipse操作MapReduce例子报错:Failed to set permissions of path: \tmp\hadoop-Jerome\mapred\staging\

    windows下Eclipse操作MapReduce例子报错: 14/05/18 22:05:29 WARN util.NativeCodeLoader: Unable to load native- ...

  3. 为什么不要在viewDidLoad方法中设置开始监听键盘通知

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 一个普遍的错误是,程序猿(媛)试图在view controll ...

  4. Dynamics CRM Import Solution Attribute Display Name description is null or empty

    在做解决方案导入的时候遇到错误,下载错误xml信息后查询报错如下:"Attribute Display Name description is null or empty",字面意 ...

  5. 在javascript里 string 和 int 类型转换

    string 转换为int 类型 (1)tostring()方法 var   x=10    a   =   x.toString() //输出为string类型 alert(typeof(a)); ...

  6. 2.QT中使用资源文件,程序打包

     1 程序中使用资源文件 A  一个QT空项目 B  右击项目,添加新文件 添加后的效果是 C  右击main.prc,选择"添加现有项",找到要使用的资源文件.最终的效果是: ...

  7. Android开发学习之路--Content Provider之初体验

    天气说变就变,马上又变冷了,还好空气不错,阳光也不错,早起上班的车上的人也不多,公司来的同事和昨天一样一样的,可能明天会多一些吧,那就再来学习android吧.学了两个android的组件,这里学习下 ...

  8. Hessian源码分析--HessianSkeleton

    HessianSkeleton是Hessian的服务端的核心,简单总结来说:HessianSkeleton根据客户端请求的链接,获取到需要执行的接口及实现类,对客户端发送过来的二进制数据进行反序列化, ...

  9. JAVA之旅(十六)——String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较

    JAVA之旅(十六)--String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较 过节耽误了几天,我们继续JAVA之旅 一.String概述 String时 ...

  10. UNIX环境高级编程——文件和目录

    一.获取文件/目录的属性信息 int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); in ...