求解2个数的最大公约数 和 求解质因数

 

  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Text;
  4
  5 namespace  GCD
  6 {
  7    class Program
  8    {
  9        static void Main(string[] args)
 10        {
 11            int b = 120;
 12            int s = 40;
 13
 14            System.Console.WriteLine("求解120和40的最大公约数:" + gcd(b, s));
 15
 16            System.Console.WriteLine("求154的质因数:");
 17            int[] zys = Sieve(154);
 18            for (int i = 0; i < zys.Length; i++)
 19            {
 20                System.Console.Write(zys[i] + " ");
 21            }

 22            System.Console.WriteLine();
 23
 24
 25        }

 26
 27        /// <summary>
 28        /// 欧几里德法求最大公约数
 29        /// </summary>
 30        /// <param name="b">较大的数</param>
 31        /// <param name="s">较小的数</param>
 32        /// <returns>两个数的最大公约数</returns>

 33        static int gcd(int b, int s)
 34        {
 35            if (s == 1)
 36            {
 37                return s;
 38            }

 39
 40            int temp = s % b;
 41
 42            if (temp == 0)
 43            {
 44                return s;
 45            }

 46
 47            return gcd(s, temp);
 48        }

 49
 50        /// <summary>
 51        /// 利用 埃拉托色尼“筛” 求解质因数
 52        /// </summary>
 53        /// <param name="n">需要求解质因数的数</param>
 54        /// <returns>该数的质因数</returns>

 55        static int[] Sieve(int n)
 56        {
 57            int a = 0;
 58            n -= 2;
 59            int[] resource = new int[n];
 60
 61            for (int i = 0; i < n; i++)
 62            {
 63                resource[i] = i + 2;
 64            }

 65
 66            //向下取整,减少循环次数。
 67            int num = (int)Math.Sqrt(n);
 68
 69            for (int i = 0; i < num; i++)
 70            {
 71                int baseNum = resource[i];
 72
 73                if (baseNum == 0)
 74                {
 75                    continue;
 76                }

 77
 78                for (int j = i + 1; j < n; j++)
 79                {
 80                    if(resource[j] == 0)
 81                    {
 82                        continue;
 83                    }

 84
 85                    if (resource[j] % baseNum == 0)
 86                    {
 87                        resource[j] = 0;
 88                        a++;
 89                    }

 90                }

 91            }

 92
 93            int[] result = new int[n - a];
 94            int index = 0;
 95            for (int i = 0; i < n; i++)
 96            {
 97                if (resource[i] != 0)
 98                {
 99                    result[index] = resource[i];
100                    index++;
101                }

102            }

103
104            return result;
105        }

106    }

107}

108

这是《算法设计与分析基础》 1.1 中的C#实现。

如果大家有自己的看法或更好的实现,大家可以一起讨论。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值