"""Check old/new census labels, without repeating large transport experiments.""" import hashlib import json from pathlib import Path import resource import sys import time def primes(limit): return [n for n in range(2, limit+1) if all(n % p for p in range(2, int(n**0.5)+1))] def census_formula(x): result = 1 for p in primes(x): if p > 2: result *= p-2 return result def count_direct(x): ps = primes(x) width = 1 for p in ps: width *= p mask = bytearray(b'\1')*width for p in ps: for residue in {0, p-2}: count = (width-1-residue)//p+1 mask[residue:width:p] = b'\0'*count return {'x': x, 'width': width, 'count': mask.count(1)} started = time.monotonic() cpu_start = time.process_time() data = json.loads(Path(sys.argv[1]).read_text()) assert hashlib.sha256(data['table_md'].encode()).hexdigest() == data['table_sha256'] rows = [] for line in data['table_md'].splitlines(): if not line.startswith('| ') or '→' not in line: continue cells = [c.strip() for c in line.split('|')[1:-1]] assert len(cells) == 8 x, q = map(int, cells[0].split(' (')[0].split(' → ')) printed = int(cells[1].replace(',', '')) old, new = census_formula(x), census_formula(x)*(q-2) rows.append({'fold': [x, q], 'printed_D_old': printed, 'expected_D_old': old, 'expected_D_new': new, 'label_matches': printed == old, 'printed_is_D_new': printed == new}) assert len(rows) == 8 wrong = [r for r in rows if not r['label_matches']] assert [r['fold'] for r in wrong] == [[13, 17], [17, 19], [19, 23]] assert all(r['printed_is_D_new'] for r in wrong) # A tiny independent integer-period sieve checks the three base counts. # It does not consume the source's C sieve, operator or transport algorithm. direct = [count_direct(x) for x in [13, 17, 19]] assert all(r['count'] == census_formula(r['x']) for r in direct) assert all(data['small_control_counts'][str(r['x'])] == r['count'] for r in direct) accepted_censuses = {int(x): count for x, count in data['return_162_censuses'].items()} assert all(count == census_formula(x) for x, count in accepted_censuses.items()) large_old_rows = [r for r in rows if r['fold'][0] in accepted_censuses] assert len(large_old_rows) == 2 assert all(r['printed_D_old'] == accepted_censuses[r['fold'][0]] for r in large_old_rows) assert rows[-1]['expected_D_new'] == 8499244879125 assert data['return_161_T29_count'] == accepted_censuses[29] == 214708725 # Sanity check that a repaired table and a generic old/new header swap differ. # Five rows are already correctly old; renaming the whole column would fail. assert sum(r['label_matches'] for r in rows) == 5 assert sum(r['printed_is_D_new'] for r in rows) == 3 corrected = [] row_index = 0 for line in data['table_md'].splitlines(keepends=True): if line.startswith('| ') and '→' in line: row = rows[row_index] if not row['label_matches']: old_cell = '| '+format(row['printed_D_old'], ',')+' |' new_cell = '| '+format(row['expected_D_old'], ',')+' |' assert line.count(old_cell) == 1 line = line.replace(old_cell, new_cell) row_index += 1 corrected.append(line) corrected_table = ('# Corrected census column from return #159\n\n' 'Source: @zemaj, return #159, Per fold table. Only the first three D(old) cells are corrected here. ' 'Other reported fields are retained, without a new transport rerun.\n\n'+''.join(corrected)) Path(sys.argv[2]).parent.joinpath('census-labels-716-corrected-table.md').write_text(corrected_table) result = {'job_id': 716, 'table_rows_checked': 8, 'incorrect_D_old_labels': wrong, 'correct_D_old_rows': 5, 'direct_small_base_censuses': direct, 'accepted_162_census_values_match_CRT': 3, 'transport_large_old_rows_match_162': 2, 'accepted_161_T29_count_matches_162': True, 'corrected_D_old_values': {str(r['fold'][0]): r['expected_D_old'] for r in wrong}, 'large_censuses_or_transport_not_rerun': True, 'scope': 'Census metadata and necessary copying checks; no gap/inequality/asymptotic verification.'} output = json.dumps(result, indent=2, sort_keys=True)+'\n' Path(sys.argv[2]).write_text(output) if len(sys.argv) > 3: Path(sys.argv[3]).write_text(json.dumps({'wall_seconds': time.monotonic()-started, 'cpu_seconds': time.process_time()-cpu_start, 'max_rss_platform_units': resource.getrusage(resource.RUSAGE_SELF).ru_maxrss}, indent=2)+'\n') sys.stdout.write(output)