-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreedyAlgorithm.cs
168 lines (161 loc) · 5.42 KB
/
GreedyAlgorithm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
namespace Algorithms
{
internal class GreedyAlgorithm
{
internal static void Run()
{
{
HashSet<List<int>> resset = new HashSet<List<int>>();
List<int> list = new List<int>();
int n = 4;
SplitSummands(n, list, resset);
Console.WriteLine("For " + n + ", count = " + resset.Count);
foreach (var items in resset)
{
foreach (var item in items)
{
Console.Write(" " + item);
}
Console.WriteLine("");
}
}
{
HashSet<List<int>> resset = new HashSet<List<int>>();
List<int> list = new List<int>();
int n = 4;
SplitSummandsExcept(4, 3, list, resset);
Console.WriteLine("For " + n + ", count = " + resset.Count);
foreach (var items in resset)
{
foreach (var item in items)
{
Console.Write(" " + item);
}
Console.WriteLine("");
}
}
{
var l = 1680;
var h = 480;
int i = FindSmallestQuadRecurse(l, h);
Console.WriteLine("FindSmallestQuadRecurse (" + l + "," + h + "): " + i);
}
{
var l = 1680;
var h = 481;
int i = FindSmallestQuadRecurse(l, h);
Console.WriteLine("FindSmallestQuadRecurse (" + l + "," + h + "): " + i);
}
{
var l = 1680;
var h = 500;
int i = FindSmallestQuadRecurse(l, h);
Console.WriteLine("FindSmallestQuadRecurse (" + l + "," + h + "): " + i);
}
{
var l = 73*902635; // 65892355
var h = 73*4158; // 303534
int i = FindSmallestQuadRecurse(l, h);
Console.WriteLine("FindSmallestQuadRecurse (" + l + "," + h + "): " + i);
}
{
var l = 73 * 902635; // 65892355
var h = 73 * 4158; // 303534
long i = FindLowestCommonMultiple(l, h);
Console.WriteLine("FindLowestCommonMultiple (" + l + "," + h + "): " + i); // 273980412090
}
{
var l = 789;
var h = 90345;
long i = FindLowestCommonMultiple(l, h);
Console.WriteLine("FindLowestCommonMultiple (" + l + "," + h + "): " + i);
}
{
var l = 7891;
var h = 90345;
long i = FindLowestCommonMultiple(l, h);
Console.WriteLine("FindLowestCommonMultiple (" + l + "," + h + "): " + i);
}
{
var l = 8;
var h = 80;
long i = FindLowestCommonMultiple(l, h);
Console.WriteLine("FindLowestCommonMultiple (" + l + "," + h + "): " + i);
}
}
private static long FindLowestCommonMultiple(long big, long small)
{
var l1 = FindAllMultiples(big);
long nok1 = l1.Aggregate(1, (a, b) => (int)(a * b));
var l2 = FindAllMultiples(small);
List<long> nlist = new List<long>();
foreach(var item in l2)
{
if (l1.Contains(item))
{
l1.Remove(item);
}
else
{
nlist.Add(item);
}
}
long nok2 = nlist.Aggregate(1, (a, b) => (int)(a * b));
return nok1 * nok2;
}
private static List<long> FindAllMultiples(long n)
{
List<long> hs = new List<long>();
long div = 2;
while (n > 1)
{
while (n % div == 0)
{
hs.Add(div);
n = n / div;
}
div++;
}
return hs;
}
private static int FindSmallestQuadRecurse(int l, int h)
{
var diff = l % h;
if (diff == 0)
return h;
if (l < 1)
return -1;
return FindSmallestQuadRecurse(h, diff);
}
private static void SplitSummandsExcept(int sum, int exc, List<int> list, HashSet<List<int>> resset)
{
if (sum == 0)
{
resset.Add(list);
return;
}
foreach (int item in Enumerable.Range(1, sum))
{
if (item == exc)
continue;
var narr = new List<int>(list);
narr.Add(item);
SplitSummandsExcept(sum - item, exc, narr, resset);
}
}
private static void SplitSummands(int sum, List<int> list, HashSet<List<int>> resset)
{
if(sum == 0)
{
resset.Add(list);
return;
}
foreach (int item in Enumerable.Range(1, sum))
{
var narr = new List<int>(list);
narr.Add(item);
SplitSummands(sum - item, narr, resset);
}
}
}
}