-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubset.c
208 lines (160 loc) · 5.82 KB
/
Subset.c
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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "Subset.h"
// Given a the first two items in a triple (A, B, C),
// we can index by A and by B, and get a Bitstring with
// a bit sent for each value of C which creates a triplet
// which cannot be in the subset, due to forming a square
Bitstring InvalidTable[N+1][N+1];
// Bits to represent the values we know will be in the
// subset. These values will be primes greater than N/2
Bitstring Knowns;
int KnownLength = 0;
int main(){
setupInvalidTable();
setupKnownBitstring();
// Stack Size is, at a max, the sum of [1..N]
Subset * stack = malloc(sizeof(Subset) * STACK_SIZE);
int stackindex = 0;
printf("Known Values: ");
printBitstring(Knowns);
printf("\n");
// Search for a subset of the biggest size possible, starting with
// size N. We skip over the values we already know will be in the set
for (int target = N - KnownLength; target > 0; target--){
// Add a Subset starting with each value [1..N] which
// is not known to be in the final largest subset
for (int i = N; i > 0; i--)
if (!getBit(Knowns, i))
newSubset(&(stack[stackindex++]), i);
// Exhaust the stack
while (stackindex != 0){
Subset top = stack[--stackindex];
if (top.length == 1)
printf("\rSearching |S| = %d, S[0] = %d", target + KnownLength, top.values[0]);
// Solution found with proper size
if (top.length == target){
printSolution(&top);
exit(0);
}
// Calculate the best case. ReleventExcluded gives the number
// of numbrs between the current max of the subset, and N, that
// are not currently known to be unable to be added to the set
int bestcase = top.length + N
- top.values[top.length-1]
- relevantExcluded(&top);
if (bestcase < target)
continue;
// For each value from [maxOfTop+1..N], make a new Subset
// as an extension of top. Skip over values which are known
// to be invalid for a final subset.
for (int i = N; i > top.values[top.length-1]; i--)
if (!getBit(top.invalid, i))
extendSubset(&(stack[stackindex++]), &top, i);
}
printf("\n");
}
}
/* Functions for Bitstrings */
void printBitstring(Bitstring bs){
for (int i = 1; i < (CHUNKS * 64); i++)
if (getBit(bs, i)) printf("%d ", i);
printf("\n");
}
void setBit(Bitstring bs, int bit){
int index = bit / 64;
int offset = bit % 64;
uint64_t mask = 1ull << offset;
bs[index] |= mask;
}
int getBit(Bitstring bs, int bit){
int index = bit / 64;
int offset = bit % 64;
uint64_t mask = 1ull << offset;
return (bs[index] & mask) >> offset;
}
void orBitstrings(Bitstring lhs, Bitstring rhs){
for (int i = 0; i < CHUNKS; i++)
lhs[i] |= rhs[i];
}
/* Functions for Subset structs */
void newSubset(Subset * ss, int value){
memcpy(ss->invalid, Knowns, CHUNKS * sizeof(uint64_t));
ss->length = 1;
ss->values[0] = value;
}
void extendSubset(Subset * ss, Subset * base, int value){
memcpy(ss->invalid, base->invalid, CHUNKS * sizeof(uint64_t));
ss->length = base->length + 1;
memcpy(ss->values, base->values, N * sizeof(int));
ss->values[base->length] = value;
// Update the invalid bitstring to account for the new value
for (int i = 0; i < base->length; i++)
orBitstrings(ss->invalid, InvalidTable[ss->values[i]][value]);
}
int relevantExcluded(Subset * ss){
int start = 1 + ss->values[ss->length-1];
int partialindex = start / 64;
int partialoffset = start % 64;
// Only run popcount on bits [start..N]
int count = __builtin_popcountll(ss->invalid[partialindex] >> partialoffset);
for (int i = partialindex + 1; i < CHUNKS; i++)
count += __builtin_popcountll(ss->invalid[i]);
return count;
}
/* Functions for identifiying invalid triplets */
int formsSquare(int a, int b, int c){
int product = a * b * c;
int root = (int)sqrt(product);
return root * root == product;
}
void setupInvalidTable(){
for (int a = 1; a <= N; a++)
for (int b = 1; b <= N; b++)
for (int c = 1; c <= N; c++)
if (formsSquare(a, b, c))
setBit(InvalidTable[a][b], c);
}
/* Functions for indentifying known values */
int isPrime(int x){
if (x == 1) return 0;
if (x == 2) return 1;
for (int i = 2; i < x; i++)
if (x % i == 0) return 0;
return 1;
}
void setupKnownBitstring(){
for (int i = 1 + N/2; i <= N; i++){
if (isPrime(i)){
setBit(Knowns, i);
KnownLength += 1;
}
}
}
/* Printing the Knowns with the found subset */
void printSolution(Subset * ss){
int magnitude = ss->length + KnownLength;
int i, j, temp, idx = 0, answer[magnitude];
// Concan the known values with the found subset
for (i = 1; i <= N; i++)
if (getBit(Knowns, i))
answer[idx++] = i;
memcpy(answer + idx, ss->values, sizeof(int) * ss->length);
// Sort the subset we have greated
for (i = 0; i < magnitude; i++){
for (j = i+1; j < magnitude; j++){
if (answer[j] < answer[i]){
temp = answer[i];
answer[i] = answer[j];
answer[j] = temp;
}
}
}
printf("|S| = %d; S = {", magnitude);
for (i = 0; i < magnitude; i++)
printf(" %d ", answer[i]);
printf("}\n");
}