Groups | Search | Server Info | Login | Register


Groups > comp.lang.c > #396566

Re: Collatz Conjecture proved.

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Collatz Conjecture proved.
Date 2026-02-03 03:58 -0800
Organization A noiseless patient Spider
Message-ID <86h5ryjaxe.fsf@linuxsc.com> (permalink)
References <96ed450bdb96454829f94b79519afa93595b27c1.camel@gmail.com> <jfi.697DFF78.42.fortytwo@xs4all.nl>

Show all headers | View raw


Jan van den Broek <fortytwo@xs4all.nl> writes:

> I'm not intendig to proof anything, but I wrote this some time ago when being
> bored.
>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #include <assert.h>
> #include <ctype.h>
>
> #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<argc; ++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.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: Collatz Conjecture proved. Jan van den Broek <fortytwo@xs4all.nl> - 2026-01-31 14:11 +0100
  Re: Collatz Conjecture proved. Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-02-03 03:58 -0800
    Re: Collatz Conjecture proved. Jan van den Broek <balglaas@dds.nl> - 2026-02-03 21:27 +0000

csiph-web