-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
387 lines (309 loc) · 11.8 KB
/
main.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <iostream>
#include <limits>
#include <bitset.h>
#include <cmath>
#include <thread>
#include <atomic>
unsigned int threadNum = 8;
std::chrono::time_point<std::chrono::system_clock> start;
std::atomic<unsigned long long> total(0);
void nextNum(Bitset& bnum, unsigned int ndim) {
if(ndim > 1444 //we don't need to go higher than 1444
|| bnum.Size() < ndim)//num of bits <= dims?
throw std::runtime_error("Dimension too high!");
int lastZero = -1;//-1 stays if no one/zero found
int lastOne = -1;
//if next position is still valid
while(lastZero + 1 < ndim
&& !bnum.Get(lastZero + 1)) //and is a zero
++lastZero;
//start searching for a one right after the last zero
lastOne = lastZero;
//protect from consecutive ones
while(lastOne + 1 < ndim //if next pos still valid
&& bnum.Get(lastOne + 1)) //and is a one
++lastOne;
if(lastOne == -1//if there is no one next to a zero
|| lastOne + 1 >= ndim)//or one is at the end
throw std::runtime_error("Overflow!");
//how many ones to shift to the end
int numOnesToShift = lastOne;
//if there were zeros before the found one
if(lastZero != -1)//subtract them from num ones
numOnesToShift = lastOne - lastZero - 1;
for(int i = 0; i < lastOne; ++i) {
//if: set all ones at the beginning
//else: all zeros between the last one
//and the beginning 1es
if(i < numOnesToShift)
bnum.Set(i, true);
else
bnum.Set(i, false);
}
bnum.Set(lastOne, false);//change last one
bnum.Set(lastOne + 1, true);//with zero next to it
}
/*
//ones = Anzahl Einsen
//ndim = Anzahl Dimensionen
for(int i = 0; i < ndim - ones; i++) {
for(int j = i + 1; j < ndim - (ones - 1); j++) {
...
for(int m = l + 1; m < ndim - 2; m++) {
for(int n = m + 1; n < ndim - 1; n++) {
//commands
}
}
}
}
*/
void nextNum(std::vector<int>& ones, unsigned int ndim) {
if(ndim == 0 || ones.size() == 0)
throw std::runtime_error("Invalid parameters!");
else if(ones[0] >= ndim - ones.size())
throw std::runtime_error("Maximum reached!");
//always start in most inner "loop"
unsigned int loopPointer = ones.size() - 1;
//reset of all inner variables required?
bool triggerReset = false;
//break if outest loop has reached the maximum
while(loopPointer > 0
|| ones[0] <= ndim - ones.size()) {
//default operation: increase index in curr loop
++ones[loopPointer];
//if limit is not reached yet
if(ones[loopPointer]
<= ndim - (ones.size() - loopPointer))
break;//nothing else to do, loop still valid
else {//go up until you find a still valid loop
//as soon as we found one, we need to
//set all inner loops to their start value
triggerReset = true;
//only decrease if not already in outest loop
if(loopPointer > 0)
--loopPointer;
}
}
//if reset triggered and the max value has not
//been reached set all following bits back ascending
if(triggerReset && ones[0] <= ndim - ones.size()) {
//invalid values should be impossible
//due to the limits of each loop
for(unsigned int tLoop = loopPointer;
tLoop < ones.size() - 1;
++tLoop)
ones[tLoop + 1] = ones[tLoop] + 1;
}
}
void setOnes(unsigned int numOnes, Bitset& bs) {
bs.Clear();
for(unsigned int i = 0; i < numOnes; ++i)
bs.Set(i);
}
void findSolution(unsigned int threadID) {
for(unsigned int n = 38; n <= 1444; ++n) { //dimensions
std::cout << n << std::endl;
unsigned int best = 0;
// all combinations of numbers of set bits in a and b which are multiplied >= 1443
for(unsigned int onesInA = 38; onesInA <= n; ++onesInA) {//38 = notw. min. Dimension
std::cout << "onesInA: " << onesInA << std::endl;
Bitset a(n);
setOnes(onesInA, a);//"smallest" vector with onesInA bits is with all bits set on the right
/*std::vector<int> aVec(onesInA);
for(int i = 0; i < aVec.size(); i++) //{0,1,2,...
aVec[i] = i;*/
try {
for(unsigned int i = 0; i < threadID; ++i) //start with different bitsets -> after that every threadNum-th
nextNum(a, n);
}
catch(std::runtime_error& ex) {
continue;
}
//a.Clear();
//a.Set(aVec);
//we don't have to go through all possible vectors with onesInA ones if we can't find an appropriate onesInB
bool foundPossibleOnesInB = false;
while(true) {//go through all a vectors with onesInA ones
unsigned int roundUp = 1443 % onesInA == 0 ? 0 : 1;
//make the "which are multiplied >= 1443" part sure with choosing an appropriate onesInB
for(unsigned int onesInB = 1443 / onesInA + roundUp;
onesInB <= n;
++onesInB) {
if(onesInA * onesInB - (onesInA + onesInB - n) * (onesInA + onesInB - n) < 1443)
continue;
//std::cout << "onesInB: " << onesInB << std::endl;
foundPossibleOnesInB = true;//if we don't find an appropriate onesInB, this number of onesInA won't work in any combination -> break
Bitset b(n);
setOnes(onesInB, b);
/*std::vector<int> bVec(onesInA);
for(int i = 0; i < bVec.size(); i++) //{0,1,2,...
bVec[i] = i;
b.Clear();
b.Set(bVec);*/
while(true) {
//std::cout << b.to_string() << "\n";
unsigned int absProd = onesInA * onesInB;
unsigned int scalarProd = a.CountAnd(b);
unsigned int res = absProd - scalarProd * scalarProd;
unsigned long long t = total.fetch_add(1);
if(t % 1000000000 == 0) {
std::cout << "processed: " << t << std::endl
<< "secs: " << std::chrono::duration<double>(std::chrono::system_clock::now() - start).count() << std::endl
<< "res: " << res << std::endl
<< "a: " << a.to_string() << std::endl//only show relevant part of bitset
<< "b: " << b.to_string() << std::endl;
}
if(res < 1444)
best = std::max(best, res);
if(res == 1443) {
std::cout << "N: " << n << std::endl
<< "a: " << a.to_string() << std::endl//only show relevant part of bitset
<< "b: " << b.to_string() << std::endl;
//return;
}
try {
nextNum(b, n);
//b.Set(bVec);
}
catch(std::runtime_error& ex) {
break;
}
}
}
if(!foundPossibleOnesInB)
break;
try {
for(unsigned int i = 0; i < threadNum; ++i)
nextNum(a, n);
//a.Clear();
//a.Set(aVec);
}
catch(std::runtime_error& ex) {
break;
}
}
if(best > 0)
std::cout << "Best in " << n << " with |a|=" << onesInA << ": " << best << std::endl;
}
}
}
void findFirstMax() {
for(int n = 1; n <= 1444; ++n) {//dimensions
//starting with 38 would work, too
// -> others break because >n
for(unsigned int onesInA = 1;
onesInA <= n;
++onesInA) {
//1 if we need to round up
//because there is a remaining
unsigned int roundUp = 1;
//true if there is no remaining
if(1443 % onesInA == 0)
roundUp = 1;
unsigned int newStart = 1443
/ onesInA
+ roundUp;
for(unsigned int onesInB = newStart;
onesInB <= n;
++onesInB) {
unsigned int scalMin = onesInA
+ onesInB
- n;
unsigned int max = onesInA * onesInB
- scalMin * scalMin;
if(max >= 1443) {
std::cout << "N: " << n
<< " A: " << onesInA
<< " B: " << onesInB
<< " - " << max
<< std::endl;
return;
}
}
}
}
}
void nextNumBenchmarkA(unsigned int ndim, unsigned int ones) {
Bitset a(ndim);
std::vector<int> aVec(ones);
for(int i = 0; i < aVec.size(); i++) //{0,1,2,...
aVec[i] = i;
auto st = std::chrono::system_clock::now();
try {
while(true) {
nextNum(aVec, ndim);
a.Clear();//ggf. zum Testen ausschalten
a.Set(aVec);//ggf. zum Testen ausschalten
}
}
catch(std::runtime_error& ex) {
std::cout << "A took secs: " << std::chrono::duration<double>(std::chrono::system_clock::now() - st).count() << std::endl;
return;
}
}
void nextNumBenchmarkB(unsigned int ndim, unsigned int ones) {
Bitset b(ndim);
setOnes(ones, b);
auto st = std::chrono::system_clock::now();
try {
while(true)
nextNum(b, ndim);
}
catch(std::runtime_error& ex) {
std::cout << "B took secs: " << std::chrono::duration<double>(std::chrono::system_clock::now() - st).count() << std::endl;
return;
}
}
void nextNumTest() {
std::cout << "6.2: " << std::endl;
unsigned int num = 0;
std::vector<int> v = {0, 1, 2};
Bitset test(5);
test.Clear();
test.Set(v);
std::cout << num++ << ": " << test.to_string() << std::endl;
while(true) {
try {
nextNum(v, 5);
test.Clear();
test.Set(v);
std::cout << num++ << ": " << test.to_string() << std::endl;
}
catch(std::runtime_error& ex) {
std::cout << ex.what() << std::endl;
break;
}
}
std::cout << "6.1:" << std::endl;
num = 0;
std::vector<int> v2 = {0, 1, 2};
test.Clear();
test.Set(v2);
std::cout << num++ << ": " << test.to_string() << std::endl;
while(true) {
try {
nextNum(test, 5);
std::cout << num++ << ": " << test.to_string() << std::endl;
}
catch(std::runtime_error& ex) {
std::cout << ex.what() << std::endl;
break;
}
}
}
int main(int argc, char const* argv[]) {
start = std::chrono::system_clock::now();
if(argc > 1)
threadNum = std::atoi(argv[1]);
std::cout << "First possibly working max found at:" << std::endl;
findFirstMax();
std::vector<std::thread> thrs;
for(unsigned int i = 0; i < threadNum; ++i) {
thrs.push_back(std::thread([ = ]() {
findSolution(i);
}));
}
for(unsigned int i = 0; i < threadNum; ++i)
thrs[i].join();
return 0;
}