求解2个数的最大公约数 和 求解质因数
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace 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
2using System.Collections.Generic;
3using System.Text;
4
5namespace 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#实现。
如果大家有自己的看法或更好的实现,大家可以一起讨论。