Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #10287
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.programming |
| Subject | Re: Long winter evenings |
| Date | 2019-11-05 10:42 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <qprg6m$6ec$1@dont-email.me> (permalink) |
| References | <qppnk6$584$1@dont-email.me> <qpq5r8$t8i$1@dont-email.me> <qpq6hk$1nl$1@dont-email.me> |
On 04/11/2019 22:51, Richard Heathfield wrote:
> On 04/11/2019 21:39, David Brown wrote:
>> On 04/11/2019 18:37, Richard Heathfield wrote:
>>> I have just written a program in C, which for reasons that will
>>> remain shrouded in the mists of wossname I wanted to be as short as
>>> possible. The program solves a highly specific and relatively
>>> antiquated problem.
>>>
>>> Now that the nights are drawing in, some of you might be at a loose
>>> end and wish for something to help you pass the time.
>>>
>>> So here's the puzzle, which is in two parts:
>>>
>>> (1) What, *exactly*, does the program do? That is, what is the
>>> significance of the printed result?
>>
>> Apart from a "newbie" award at the IOCCC? I don't know yet.
>
> Oh, it wasn't intended to be obfuscated. (You can try for obfuscated if
> you like - I'm not a genius at it like some of the IOCCC guys, but when
> I'm trying I can generally put three or four levels of obfusc in there
> somewhere.)
The code is initially incomprehensible, but not really obfuscated -
that's why you only get a newbie award :-)
>
>>
>>>
>>> (2) Is there a way to make the program shorter, without compromising
>>> its rigorous adherence to ISO C90? (See below.)
>>>
>>> #include <stdio.h>
>>> int r(int*c){int f=0;if(*c%5==1)*c/= 5,*c*=4,f=1;return f;}
>>
>> Surely "f" can be eliminated and replaced with a tertiary operator
>> expression?
>>
>> int r(int*c){return(*c%5==1)?*c/= 5,*c*=4,1:0;}
>>
>> And there is a redundant space in "*c/= 5".
>
> Damn! I thought I'd caught them all.
>
>>
>>> int t(int c){int f=1,i;for(i=0;f&&i<5;i++)f=r(&c);return f*!(c%5);}
>>
>> The call to "r" could then be inlined
>>
>>> int main(void){int n=0,d=0;while(!d)d=t(++n);printf("%d\n",n);return 0;}
>>
>> And the call to "t" could be inlined.
>>
>> #include <stdio.h>
>> int main(void){int
>> n=0,d=0,c,f,i;while(!d){++n;c=n,f=1;for(i=0;f&&i<5;i++)f=(c%5==1)?c/=5,c*=4,1:0;d=f*!(c%5);};printf("%d\n",n);return
>> 0;}
>>
>> That's 160 characters, compared to the original 221.
>
> An astonishing reduction. The inlining was the big win - and, bizarrely,
> I moved those bits out to functions because doing so had the effect of
> dramatically *reducing* the size! So it's quite pleasing to see them
> moved back in again and gaining a further reduction in the process.
>
Sometimes inlining is a win on size, sometimes not - it applies to
source code as much as compiler optimisations.
>> And I guess this is cheating too?
>>
>> #include <stdio.h>
>> int main(void){printf("3121\n;return 0;}
>
> Yes! That is quite definitely cheating! :-)
>
> And I'm surprised at you: puts("3121") is a mere 75% of the size that
> printf("3121\n would be if you'd closed it off properly with ")
Ah, you spotted my intentional mistake to check you were paying
attention, /and/ my exercise left for the reader :-)
>
> The problem with printing 3121 is that it removes the calculation, which
> is intrinsic to establishing what the program is actually for.
>
>>> If nobody gets it within a week or so, I'll post the solution to (1).
>>> (If I could think of a solution to (2), I'd have written it that way
>>> to start with!)
>>>
>>
>> I still don't know what it does...
>
> Phone home and tell them you'll be late.
>
Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Long winter evenings Richard Heathfield <rjh@cpax.org.uk> - 2019-11-04 17:37 +0000
Re: Long winter evenings David Brown <david.brown@hesbynett.no> - 2019-11-04 22:39 +0100
Re: Long winter evenings Richard Heathfield <rjh@cpax.org.uk> - 2019-11-04 21:51 +0000
Re: Long winter evenings David Brown <david.brown@hesbynett.no> - 2019-11-05 10:42 +0100
Re: Long winter evenings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 22:24 +0000
Re: Long winter evenings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 22:19 +0000
Re: Long winter evenings Richard Heathfield <rjh@cpax.org.uk> - 2019-11-04 22:30 +0000
Re: Long winter evenings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 23:59 +0000
Re: Long winter evenings Richard Heathfield <rjh@cpax.org.uk> - 2019-11-05 01:03 +0000
Re: Long winter evenings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-05 04:01 +0000
Re: Long winter evenings Richard Heathfield <rjh@cpax.org.uk> - 2019-11-05 07:42 +0000
Re: Long winter evenings Melzzzzz <Melzzzzz@zzzzz.com> - 2019-11-04 23:00 +0000
Re: Long winter evenings Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-06 04:58 -0800
csiph-web