前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 最大公约数

原 最大公约数

作者头像
魂祭心
发布2018-05-17 16:00:15
6600
发布2018-05-17 16:00:15
举报
文章被收录于专栏:魂祭心魂祭心

代码:

穷举法

代码语言:javascript
复制
        //穷举法
        public static Int32 GetMaxCommonDivisorWithExhaustion(Int32 biggerNum, Int32 smallerNum)
        {
            Int32 maxDivisor = 0;
            for (int index = 1; index < smallerNum / 2; index++)
            {
                if (biggerNum % index == 0 && smallerNum % index == 0)
                {
                    maxDivisor = index;
                }
            }
            return maxDivisor;
        }

更相减损法

代码语言:javascript
复制
        //更相减损法(递归)
        public static Int32 GetMaxCommonDivisorWithGxjsRecursionRecursion(Int32 biggerNum, Int32 smallerNum)
        {
            return biggerNum != smallerNum ? (smallerNum > (biggerNum - smallerNum) ? GetMaxCommonDivisorWithGxjsRecursionRecursion(smallerNum, biggerNum - smallerNum) : GetMaxCommonDivisorWithGxjsRecursionRecursion(biggerNum - smallerNum, smallerNum)) : biggerNum;
        }
        //更相减损法(非递归)
        public static Int32 GetMaxCommonDivisorWithGxjsRecursionNorecursion(Int32 biggerNum, Int32 smallerNum)
        {
            while (biggerNum != smallerNum)
            {
                if (smallerNum > biggerNum - smallerNum)
                {
                    int temp = smallerNum;
                    smallerNum = biggerNum - smallerNum;
                    biggerNum = temp;
                }
                else
                {
                    biggerNum -= smallerNum;
                }
            }
            return biggerNum;
        }

辗转相除法

代码语言:javascript
复制
        //辗转相处法 (递归)
        public static Int32 GetMaxCommonDivisorWithGcdRecursion(Int32 biggerNum, Int32 smallerNum)
        {
            return biggerNum % smallerNum == 0 ? smallerNum : GetMaxCommonDivisorWithGcdRecursion(smallerNum, biggerNum % smallerNum);
        }
        //辗转相处法 (非递归)
        public static Int32 GetMaxCommonDivisorWithGcdNorecursion(Int32 biggerNum, Int32 smallerNum)
        {
            while (biggerNum % smallerNum != 0)
            {
                int temp = biggerNum;
                biggerNum = smallerNum;
                smallerNum = temp % smallerNum;
            }
            return smallerNum;
        }

测试代码:

代码语言:javascript
复制
   Stopwatch sw = new Stopwatch();
            sw.Reset();
            sw.Start();
            {
                for (int i = 0; i < 100000000; i++)
                {
                    GetMaxCommonDivisorWithGxjsRecursionRecursion(260, 60);
                }
            }
            sw.Stop();
            Console.WriteLine("更相减损法(递归):" + sw.ElapsedMilliseconds);
            sw.Reset();
            sw.Start();
            {
                for (int i = 0; i < 100000000; i++)
                {
                    GetMaxCommonDivisorWithGxjsRecursionNorecursion(260, 60);
                }
            }
            sw.Stop();
            Console.WriteLine("更相减损法(非递归):" + sw.ElapsedMilliseconds);


            sw.Reset();
            sw.Start();
            {
                for (int i = 0; i < 100000000; i++)
                {
                    GetMaxCommonDivisorWithGcdRecursion(260, 60);
                }
            }
            sw.Stop();
            Console.WriteLine("辗转相除法:" + sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();
            {
                for (int i = 0; i < 100000000; i++)
                {
                    GetMaxCommonDivisorWithGcdNorecursion(260, 60);
                }
            }
            sw.Stop();
            Console.WriteLine("辗转相除法:(非递归)" + sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();
            {
                for (int i = 0; i < 100000000; i++)
                {
                    GetMaxCommonDivisorWithExhaustion(260, 60);
                }
            }
            sw.Stop();
            Console.WriteLine("穷举法:" + sw.ElapsedMilliseconds);

测试结果:

结论:

更相减损法>辗转相除法>穷举法

非递归普遍比递归表现的更好

更相减损法仍旧有改进的余地,辗转相除法的优势是除了之后余数不会比除数更大,更像减损法减了之后仍旧需要比较。

和我这种写法或许也有关系,进去的值已经判断了大小了,如果换成无序的传进去参数,或许会不一样。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档