Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.programming Subject: Re: Long winter evenings Date: Mon, 04 Nov 2019 22:19:39 +0000 Organization: A noiseless patient Spider Lines: 61 Message-ID: <87k18fjnd0.fsf@bsb.me.uk> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="e0eb4ca205b0748ecaaa72661425f5df"; logging-data="11171"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX183gFiveNz+IYBnsgBYHydbnFhF3JXo3bc=" Cancel-Lock: sha1:VCoT2+NHUw8cQeViiPsYNleXIXg= sha1:f2gsDOULt/4/zdfNbeGcHZ6Hwa0= X-BSB-Auth: 1.f42623f16215ffd06599.20191104221939GMT.87k18fjnd0.fsf@bsb.me.uk Xref: csiph.com comp.programming:10279 Richard Heathfield writes: > 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? These are rather odd questions. I know the answer you want, but if someone can find some other significance for the output, you'd have to accept that answer. I think you mean something along the lines of "what is the significance of the output that explains the method of calculation"? I won't say what I think you intend the answer to be, but I will say that the output is the first number in a infinite sequence that continues 18746, 34371, 49996, 65621, 81246, ... > (2) Is there a way to make the program shorter, without compromising > its rigorous adherence to ISO C90? (See below.) Yes. Again, a bit odd. How much can we change? Preserving the logic and method of your calculation I can get the size down from 220 characters to 157, but I'm sure that is not the lowest. Changing the logic, but sticking with the "purpose" (rather than just printing the answer), there are endless choices... For example: #include int n;int main(void){while((++n*1024l-8404)%15625);return!printf("%d\n",n);} (96 characters) > #include > int r(int*c){int f=0;if(*c%5==1)*c/= 5,*c*=4,f=1;return f;} > int t(int c){int f=1,i;for(i=0;f&&i<5;i++)f=r(&c);return f*!(c%5);} > int main(void){int n=0,d=0;while(!d)d=t(++n);printf("%d\n",n);return 0;} > > Four obvious shortenings are excluded: > A. Lose the space between "#include" and ""; > B. Lose the second and third newlines; > C. Lose "void"; > D. Lose "return 0;" (Everyone can answer yes to Q2 if they spot the space between /= and 5!) > 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 find that second part hard to believe. -- Ben.