Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Collatz Conjecture proved. Date: Tue, 03 Feb 2026 03:58:37 -0800 Organization: A noiseless patient Spider Lines: 137 Message-ID: <86h5ryjaxe.fsf@linuxsc.com> References: <96ed450bdb96454829f94b79519afa93595b27c1.camel@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Tue, 03 Feb 2026 11:58:44 +0000 (UTC) Injection-Info: dont-email.me; posting-host="72e789f1a61a439ff5daaffc25398413"; logging-data="1520319"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18UbUMedF/0ip8QmYxUMIRmi+5/Sh3et4o=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:VNbifM1nZDfnos55VBqnQZ8522k= sha1:0oyV0z6WU1+/B+W8X6j8lV9bdIY= Xref: csiph.com comp.lang.c:396566 Jan van den Broek writes: > I'm not intendig to proof anything, but I wrote this some time ago when being > bored. > > #include > #include > #include > #include > #include > > #define N 1024 > > int check(char *s) > { > int result=1; > while (*s) > { > if (!isdigit(*s++)) > { > result=0; > } > } > return(result); > } > > char *insert(char *s,char c,int *l) > { > int i; > assert (*l<(N-1)); > > for (i=*l; i; --i) > { > s[i]=s[i-1]; > } > s[0]=c; > ++(*l); > s[*l]='\0'; > return(s); > } > > char *even(char *s,int *l) > { > int c,carry,i; > char *copy; > > copy=malloc(*l+1); > assert(copy!=NULL); > copy[*l]='\0'; > carry=0; > for (i=0; i<*l; ++i) > { > c=s[i]-'0'+carry; > carry=0; > if (c&1) > { > carry=10; > c-=1; > } > c=c/2; > copy[i]=c+'0'; > } > i=0; > while (copy[i]=='0') > { > ++i; > } > (*l)-=i; > strcpy(s,copy+i); > free(copy); > return(s); > } > > char *odd(char *s,int *l) > { > int c,carry,i; > char *copy; > > copy=malloc(*l+2); > assert(copy!=NULL); > carry=0; > copy[*l]='\0'; > for (i=*l; i; --i) > { > c=3*(s[i-1]-'0')+carry; > if (i==*l) > { > ++c; > } > copy[i-1]=(c%10)+'0'; > carry=(c-(c%10))/10; > } > while(carry) > { > c=carry%10; > insert(copy,c+'0',l); > carry=(c-(c%10))/10; > } > strcpy(s,copy); > free(copy); > return(s); > } > > int main(int argc,char *argv[]) > { > int i,l; > char data[N]; > > for (i=1; i { > strcpy(data,argv[i]); > if (check(data)) > { > puts(data); > l=strlen(data); > while (strcmp(data,"1")) > { > if (data[l-1]&1) > { > odd(data,&l); > } > else > { > even(data,&l); > } > puts(data); > } > } > } > } Cute. The code can be made simpler and shorter, with updates being done in place rather than having to do malloc()/copy/free() each time, if the number is stored right justified in the data[] buffer rather than left justified.