Article 17357 of comp.lang.c: Path: intelisc!omepd!mipos3!intelca!oliveb!apple!rutgers!att!alberta!c415-23 From: c415-23@alberta.UUCP (Morris Barbara L) Newsgroups: comp.lang.c Subject: programming challenge (silly) Message-ID: <2102@jasper.UUCP> Date: 4 Mar 89 08:09:46 GMT Distribution: na Organization: U. of Alberta, Edmonton, AB Lines: 21 A friend and I tried to mimic the behaviour of the following program in the fewest characters possible. The best we were able to do was 98 characters (result of wc). Can anyone beat this? Email for our solution. main() { int n; scanf( "%d", &n ); if ( n < 1 || n >= 10 ) printf( "error\n" ); else printf( "answer is %d\n", factorial( n ) ); } int factorial( n ) int n; { if ( n == 1 ) return( n ); else return( n * factorial( n - 1 ) ); } ---------------------------------------------------------------------- My answer: main(){int n;scanf("%d",&n);(n<1||n>9)?puts("error"):printf("answer is %d\n",f(n));}f(n){return n==1?n:n*f(n-1);} ---------------------------------------------------------------------- From omepd!mipos3!intelca!oliveb!ames!ig!bionet!agate!saturn!kjell Mon Mar 13 12:52:43 PST 1989 Article 17439 of comp.lang.c: Path: intelisc!omepd!mipos3!intelca!oliveb!ames!ig!bionet!agate!saturn!kjell >From: kjell@saturn.ucsc.edu (Kjell Post) Newsgroups: comp.lang.c Subject: Re: programming challenge (silly) Message-ID: <6619@saturn.ucsc.edu> Date: 7 Mar 89 13:21:00 GMT References: <2102@jasper.UUCP> <9789@smoke.BRL.MIL> <3115@homxb.ATT.COM> Reply-To: kjell@saturn.ucsc.edu (Kjell Post) Distribution: na Organization: University of California, Santa Cruz Lines: 18 In article <3115@homxb.ATT.COM> cjn@homxb.ATT.COM (C.NORTHRUP) writes: > >------------------------------------------ > >Kevin Meier from AT&T Bell Labs completed this in approx 15 minutes using >only 96 characters. I will submit his solution in a couple of days. >Gives others a chance.... I have 91 now but am not sure about portability of main(n,m) and printf. main(n,m){scanf("%d\n",&m);while(m>1&m<10)n*=m--; printf(m-1?"error\n":"answer is %d\n",n);} -- For athletes and programmers, ! Kjell E. Post a woman is the end of their career. ! CIS/CE ! University of California, Santa Cruz -- A.Wickberg ! Email: kjell@saturn.ucsc.edu From littlei!ogccse!blake!uw-beaver!cornell!mailrus!nrl-cmf!ames!pasteur!agate!saturn!kjell Mon Mar 13 12:54:23 PST 1989 Article 17475 of comp.lang.c: Path: intelisc!littlei!ogccse!blake!uw-beaver!cornell!mailrus!nrl-cmf!ames!pasteur!agate!saturn!kjell >From: kjell@saturn.ucsc.edu (Kjell Post) Newsgroups: comp.lang.c Subject: Re: programming challenge (silly) Message-ID: <6639@saturn.ucsc.edu> Date: 9 Mar 89 02:28:29 GMT References: <2102@jasper.UUCP> <2128@laidbak.UUCP> Reply-To: kjell@saturn.ucsc.edu (Kjell Post) Distribution: na Organization: University of California, Santa Cruz Lines: 16 Posted: Wed Mar 8 18:28:29 1989 In article <2128@laidbak.UUCP> daveb@laidbak.UUCP (Dave Burton) writes: >> Can anyone beat [98 characters]? > >We got down to 103 characters, "portably". >On a Sun, I got 96 characters - but this is implementation dependent. >-- Count this: main(n,m){for(scanf("%d",&m);m>1&m<10;n*=m--); printf(m-1?"error\n":"answer is %d\n",n);} -- For athletes and programmers, ! Kjell E. Post a woman is the end of their career. ! CIS/CE ! University of California, Santa Cruz -- A.Wickberg ! Email: kjell@saturn.ucsc.edu From littlei!ogccse!blake!uw-beaver!mit-eddie!ll-xn!ames!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!sun!pitstop!sundc!seismo!uunet!ingr!crossgl Mon Mar 13 12:55:22 PST 1989 Article 17507 of comp.lang.c: Path: intelisc!littlei!ogccse!blake!uw-beaver!mit-eddie!ll-xn!ames!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!sun!pitstop!sundc!seismo!uunet!ingr!crossgl >From: crossgl@ingr.com (Gordon Cross) Newsgroups: comp.lang.c Subject: Re: programming challenge (silly) Message-ID: <4344@ingr.com> Date: 9 Mar 89 22:24:55 GMT References: <2102@jasper.UUCP> <9789@smoke.BRL.MIL> <3115@homxb.ATT.COM> <6619@saturn.ucsc.edu> Reply-To: crossgl@ingr.UUCP (Gordon Cross) Distribution: na Organization: Intergraph Corp. Huntsville, Al Lines: 20 Posted: Thu Mar 9 14:24:55 1989 In article <6619@saturn.ucsc.edu> kjell@saturn.ucsc.edu (Kjell Post) writes: > >I have 91 now but am not sure about portability of main(n,m) and printf. > >main(n,m){scanf("%d\n",&m);while(m>1&m<10)n*=m--; >printf(m-1?"error\n":"answer is %d\n",n);} Interesting BUT: this does NOT behave the same way as the original program in all cases!! Try running the original and your versions using: silly 5 2 The original yields: answer is 2 Yours yields: answer is 4 -- Gordon Cross UUCP: uunet!ingr!crossgl "all opinions are 111 Westminister Way INTERNET: crossgl@ingr.com mine and not those Madison, AL 35758 MA BELL: (205) 772-7842 of my employer." From omepd!mipos3!intelca!oliveb!ames!ncar!tank!uxc!uxc.cso.uiuc.edu!m.cs.uiuc.edu!s.cs.uiuc.edu!pathak Mon Mar 13 12:57:09 PST 1989 Article 17543 of comp.lang.c: Path: intelisc!omepd!mipos3!intelca!oliveb!ames!ncar!tank!uxc!uxc.cso.uiuc.edu!m.cs.uiuc.edu!s.cs.uiuc.edu!pathak >From: pathak@s.cs.uiuc.edu Newsgroups: comp.lang.c Subject: Re: programming challenge (silly) Message-ID: <207600017@s.cs.uiuc.edu> Date: 11 Mar 89 02:44:00 GMT References: <2102@jasper.UUCP> Lines: 26 Nf-ID: #R:jasper.UUCP:2102:s.cs.uiuc.edu:207600017:000:729 Nf-From: s.cs.uiuc.edu!pathak Mar 10 20:44:00 1989 In an article by kjell@saturn.ucsc.edu writes >I have 91 now but am not sure about portability of main(n,m) and printf. > >main(n,m){scanf("%d\n",&m);while(m>1&m<10)n*=m--; >printf(m-1?"error\n":"answer is %d\n",n);} Well, the code I ran the code through wc and it came out to be 92 chars. Still, it is a nice piece of code. However, the following modifications make the same code only 89 chars. main(n,m){for(scanf("%d",&m);m>1&m<10;n*=m--); printf(m-1?"error\n":"answer is %d\n",n);} Of course, you need to delete the newline after the first line. The code could further be optimized by getting rid of the extra "\n" after error and answer which would make it 85. Heeren Pathak s.cs.uiuc.edu zaphod.ncsa.uiuc.edu From omepd!mipos3!intelca!oliveb!ames!mailrus!tut.cis.ohio-state.edu!ucbvax!ucbarpa.Berkeley.EDU!klier Tue Mar 14 12:24:18 PST 1989 Article 17492 of comp.lang.c: Path: intelisc!omepd!mipos3!intelca!oliveb!ames!mailrus!tut.cis.ohio-state.edu!ucbvax!ucbarpa.Berkeley.EDU!klier >From: klier@ucbarpa.Berkeley.EDU (Pete Klier) Newsgroups: comp.lang.c Subject: Re: programming puzzle (silly) Summary: 88 char portable solution Message-ID: <28336@ucbvax.BERKELEY.EDU> Date: 10 Mar 89 02:04:48 GMT Sender: usenet@ucbvax.BERKELEY.EDU Lines: 17 *********************** main(m,n){scanf("%d",&n);for(m=n>0^n>9;n&&m*=n--;); printf(m?"Answer=%d\n":"error\n",m);} *********************** with help from Jeff Conroy (jmconroy@ernie.Berkeley.EDU) the expression "m=n>0^n>9" is equivalent to "m = (n > 0) && (n <= 9)" (check it out) so "m" is 1 (initially) iff n is within range, 0 otherwise. If m is 0 in the body of the loop, then the expression (n&&m*=n--) is 0, since (m*=n--) is 0. Otherwise, we have an ordinary factorial loop, which begins with m == 1. Pete Klier klier@ernie.Berkeley.EDU From littlei!ogccse!blake!uw-beaver!cornell!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!sun!morocco!landauer Tue Mar 14 12:27:17 PST 1989 Article 17501 of comp.lang.c: Path: intelisc!littlei!ogccse!blake!uw-beaver!cornell!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!sun!morocco!landauer >From: landauer@morocco.Sun.COM (Doug Landauer) Newsgroups: comp.lang.c Subject: Re: programming challenge (really silly) Message-ID: <93227@sun.uucp> Date: 10 Mar 89 02:00:08 GMT References: <2102@jasper.UUCP> Sender: news@sun.uucp Reply-To: landauer@sun.UUCP (Doug Landauer) Followup-To: comp.misc Distribution: na Organization: Sun Microsystems, Mountain View Lines: 49 Posted: Thu Mar 9 18:00:08 1989 > A friend and I tried to mimic the behaviour of the following program in > the fewest characters possible. The best we were able to do was 98 > characters (result of wc). Can anyone beat this? >> [a few attempts, all in C, omitted] C'mon, guys, where's your imagination? Portable? C? She said "the fewest characters possible"! (Nothing in the original challenge said it had to be in "C", which is why I've sent followups to comp.misc.) Anyway, I haven't seen any other postings that solve this silly challenge (to write a little factorial program) in as few characters as does this 76-character shell/dc/sed script: ---- dc<ed9From: cjn@homxb.ATT.COM (C.NORTHRUP) Newsgroups: comp.lang.c Subject: RE: C programming challenge Keywords: (silly) Message-ID: <3141@homxb.ATT.COM> Date: 9 Mar 89 22:22:57 GMT Organization: AT&T BL Holmdel NJ USA Lines: 41 Posted: Thu Mar 9 14:22:57 1989 Kevin Meier and Jim Weythman (from AT&T Bell Labs) have submitted an entry for the factorial program. It is 89 characters + 1 character for the new-line. The guidelines being followed were as follows: a number must be read from the keyboard if number < 1 or number > 9 then printf("error\n"); else compute factorial of number printf("answer is %d\n", answer) end if NOTE: you must use C programming language error message must be printed with the format "error\n" (or given in upper case) answer message must be printed with the format "answer is %d\n", original post said that number of characters is code is counted via wc(1). (therefore #include would add 18 characters) nothing in the original post about how long it takes to compute factorial. (so time is not a question) nothing in original post about using anything other then standard libraries (so only libc.a would be acceptable) original post used scanf("%d"...) to read in the digit(s). therefore valid data could be entered as: 45the_following_letters_are_discarded since all(?) scanf(3), in this case, would recognize the leading digits 4 and 5, original post tested for the numeric bounds of ( 0 < number < 10 ). there is no test for alphanumeric (other than scanf) BEFORE FLAMES ARE TURNED UP PLEASE NOTE: this is only to clear up some confusion. don't take it to heart, nor take it to seriously.... I have also received an entry from Kjell E. Post at the University of Calif who completed it in 91 characters, as did Robert Bruce at Berkely. charlie northrup "I am not the official score keeper" From littlei!ogccse!blake!uw-beaver!cornell!batcomputer!rpi!leah!csd4.milw.wisc.edu!bionet!agate!saturn!kjell Tue Mar 14 12:31:27 PST 1989 Article 17530 of comp.lang.c: Path: intelisc!littlei!ogccse!blake!uw-beaver!cornell!batcomputer!rpi!leah!csd4.milw.wisc.edu!bionet!agate!saturn!kjell >From: kjell@saturn.ucsc.edu (Kjell Post) Newsgroups: comp.lang.c Subject: Re: programming puzzle (silly) Message-ID: <6644@saturn.ucsc.edu> Date: 10 Mar 89 13:38:39 GMT References: <28336@ucbvax.BERKELEY.EDU> Reply-To: kjell@saturn.ucsc.edu (Kjell Post) Organization: University of California, Santa Cruz Lines: 27 Posted: Fri Mar 10 05:38:39 1989 In article <28336@ucbvax.BERKELEY.EDU> klier@ucbarpa.Berkeley.EDU (Pete Klier) writes: > >*********************** >main(m,n){scanf("%d",&n);for(m=n>0^n>9;n&&m*=n--;); >printf(m?"Answer=%d\n":"error\n",m);} >*********************** > But you changed the output. I also get an error compiling this on an ISI68K. As someone pointed out, my earlier solution breaks when arguments are supplied at the command line. I therefore propose the following as the shortest version: main(n,m){for(n=scanf("%d\n",&m);m>1&m<10;n*=m--); printf(m-1?"error\n":"answer is %d\n",n);} When written on a single line it should count to 91 characters. I would also like to add the rule that the program should compile without warnings from cc and lint (except for "scanf returns value which is always ignored" which is always ignored). -- For athletes and programmers, ! Kjell E. Post a woman is the end of their career. ! CIS/CE ! University of California, Santa Cruz -- A.Wickberg ! Email: kjell@saturn.ucsc.edu From omepd!mipos3!intelca!oliveb!ames!amdcad!crackle!tim Tue Mar 14 12:32:08 PST 1989 Article 17537 of comp.lang.c: Path: intelisc!omepd!mipos3!intelca!oliveb!ames!amdcad!crackle!tim >From: tim@crackle.amd.com (Tim Olson) Newsgroups: comp.lang.c Subject: Re: programming puzzle (silly) Message-ID: <24820@amdcad.AMD.COM> Date: 11 Mar 89 19:57:01 GMT References: <28336@ucbvax.BERKELEY.EDU> <2821@goofy.megatest.UUCP> Sender: news@amdcad.AMD.COM Reply-To: tim@amd.com (Tim Olson) Organization: Advanced Micro Devices, Inc. Sunnyvale CA Lines: 30 Summary: Expires: Sender: Followup-To: In article bader+@andrew.cmu.edu (Miles Bader) writes: | From klier@ucbarpa.Berkeley.EDU (Pete Klier): | > | > main(m,n){scanf("%d",&n);for(m=n>0^n>9;n&&m*=n--;); | > printf(m?"Answer=%d\n":"error\n",m);} | > | > with help from Jeff Conroy (jmconroy@ernie.Berkeley.EDU) | | The compiler on this machine give the following error; is it wrong? | | E "fact.c",L1/C44: (syntactic) unexpected symbol:'*=' Interesting -- the program *is* incorrect, because && has higher precedence than *= so it is parsed as: (n&&m) *= n-- which is illegal because (n&&m) is not an lvalue. Our MetaWare and Green Hills compilers catch it, but every pcc-based compiler I tried compiled it with no warnings. Looking at the assembly-language generated, it appears that it compiled as: n && (m*=n--) What do other compilers out there do? -- Tim Olson Advanced Micro Devices (tim@amd.com) From omepd!littlei!ogccse!husc6!bu-cs!buengc!bph Tue Mar 14 12:33:27 PST 1989 Article 17555 of comp.lang.c: Path: intelisc!omepd!littlei!ogccse!husc6!bu-cs!buengc!bph >From: bph@buengc.BU.EDU (Blair P. Houghton) Newsgroups: comp.lang.c Subject: Re: programming puzzle (silly) Message-ID: <2273@buengc.BU.EDU> Date: 12 Mar 89 07:09:07 GMT References: <28336@ucbvax.BERKELEY.EDU> <2821@goofy.megatest.UUCP> <24820@amdcad.AMD.COM> Reply-To: bph@buengc.bu.edu (Blair P. Houghton) Organization: Boston Univ. Col. of Eng. Lines: 38 Posted: Sat Mar 11 23:09:07 1989 In article <24820@amdcad.AMD.COM> tim@amd.com (Tim Olson) writes: >In article bader+@andrew.cmu.edu (Miles Bader) writes: >| From klier@ucbarpa.Berkeley.EDU (Pete Klier): >| > >| > main(m,n){scanf("%d",&n);for(m=n>0^n>9;n&&m*=n--;); >| > printf(m?"Answer=%d\n":"error\n",m);} > >Interesting -- the program *is* incorrect, because && has higher >precedence than *= so it is parsed as: > (n&&m) *= n-- >which is illegal because (n&&m) is not an lvalue. Our MetaWare and >Green Hills compilers catch it, >What do other compilers out there do? Depends on your attitude. 1. The cc on the uVAXen (Ultrix) passes it unnoticed. 2. While cc on the Encore (Umax) says "junk.c", line 1: Operand must be an lvalue 3. Lint always says junk.c(8): warning: precedence confusion possible: parenthesize! I view the uVAX as utilizing a heuristically-extended version of the "maximal munch" rule, where the syntax has made it obvious that the lvalue given isn't an lvalue, so it looks for the next obvious interpretation. Groovy. Whatever turns you on, man. I view the Encore as applying the tokenize-first, interpret-later version of the "maximal munch" rule. I'm a bit of a code-fascist myself, so I'd go with this one. Lint? Well, I had to give it a "-h" in order to get it to talk. Don't you just hate weasely informants? --Blair "Don't you just hate computer narcs?" From omepd!mipos3!intelca!oliveb!ames!ncar!tank!nucsrl!squires Tue Mar 14 12:34:28 PST 1989 Article 17564 of comp.lang.c: Path: intelisc!omepd!mipos3!intelca!oliveb!ames!ncar!tank!nucsrl!squires >From: squires@eecs.nwu.edu (Matthew Squires) Newsgroups: comp.lang.c Subject: Re: Re: programming challenge (silly) Message-ID: <3950013@eecs.nwu.edu> Date: 12 Mar 89 22:28:09 GMT References: <3115@homxb.ATT.COM> Organization: Northwestern U, Evanston IL, USA Lines: 12 / eecs.nwu.edu:comp.lang.c / pathak@s.cs.uiuc.edu / 8:44 pm Mar 10, 1989 / > main(n,m){for(scanf("%d",&m);m>1&m<10;n*=m--); > printf(m-1?"error\n":"answer is %d\n",n);} The expression "m>1&m<10" can be optimized by one character with the expression "m>1^m>9". > Heeren Pathak > s.cs.uiuc.edu > zaphod.ncsa.uiuc.edu From omepd!littlei!ogccse!blake!uw-beaver!cornell!mailrus!cwjcc!gatech!ncar!umigw!steve Tue Mar 14 12:35:02 PST 1989 Article 17568 of comp.lang.c: Path: intelisc!omepd!littlei!ogccse!blake!uw-beaver!cornell!mailrus!cwjcc!gatech!ncar!umigw!steve >From: steve@umigw.MIAMI.EDU (steve emmerson) Newsgroups: comp.lang.c Subject: Re: programming puzzle (silly) Summary: it's correct C Message-ID: <218@umigw.MIAMI.EDU> Date: 13 Mar 89 14:40:31 GMT References: <24820@amdcad.AMD.COM> Reply-To: steve@umigw.miami.edu (steve emmerson) Distribution: na Organization: University of Miami Lines: 36 Posted: Mon Mar 13 06:40:31 1989 In article <24820@amdcad.AMD.COM> tim@amd.com (Tim Olson) opines that the expression "n&&m*=n--" is incorrect C: >Interesting -- the program *is* incorrect, because && has higher >precedence than *= so it is parsed as: > > (n&&m) *= n-- > >which is illegal because (n&&m) is not an lvalue. I belive the correctness of a expression is determined by whether or not the expression can be *generated from* the grammer's production rules; not whether or not a given implementation of a compiler can parse it. The given expression is correct C as it can be generated by the following rules: expresion -> expression1 binop expression2 expression1 -> identifier -> n binop -> && expression2 -> lvalue asgnop expression lvalue -> identifier -> m asgnop -> *= expression -> lvalue-- -> identifier-- -> n-- Thus, expression -> n && m *= n-- Precedence rules are needed only when there is more than one way to generate a given expression and (consequently) more than one way to parse it. They appear to be unnecessary for the above expression. -- Steve Emmerson Inet: steve@umigw.miami.edu [128.116.10.1] SPAN: miami::emmerson (host 3074::) emmerson%miami.span@star.stanford.edu UUCP: ...!ncar!umigw!steve emmerson%miami.span@vlsi.jpl.nasa.gov "Computers are like God in the Old Testament: lots of rules and no mercy" From omepd!mipos3!intelca!oliveb!apple!bloom-beacon!think!gordon Tue Mar 14 12:35:45 PST 1989 Article 17578 of comp.lang.c: Path: intelisc!omepd!mipos3!intelca!oliveb!apple!bloom-beacon!think!gordon >From: gordon@think.COM (gordon linoff) Newsgroups: comp.lang.c Subject: Re: (silly challenge); Can you beat this? Message-ID: <37433@think.UUCP> Date: 13 Mar 89 19:32:45 GMT Sender: news@think.UUCP Reply-To: gordon@think.com (gordon linoff) Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 15 I wish I could take credit for this, but it is now down to 83 characters (84 with the trailing newline). Dan Perkins of Zycad Corporation came up with it. And this is it . . . main(y,x){for(scanf("%d",&x);--x%9;y*=x+1); printf(x?"error\n":"answer is %d\n",y);} The extra carriage return is for your reading pleasure, of course. --gordon@think.com Thinking Machines Corporation From omepd!mipos3!intelca!oliveb!ames!haven!adm!smoke!gwyn Tue Mar 14 12:36:29 PST 1989 Article 17590 of comp.lang.c: Path: intelisc!omepd!mipos3!intelca!oliveb!ames!haven!adm!smoke!gwyn >From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: programming puzzle (silly) Message-ID: <9846@smoke.BRL.MIL> Date: 14 Mar 89 01:56:19 GMT References: <24820@amdcad.AMD.COM> <218@umigw.MIAMI.EDU> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 21 In article <218@umigw.MIAMI.EDU> steve@umigw.miami.edu (steve emmerson) writes: >In article <24820@amdcad.AMD.COM> tim@amd.com (Tim Olson) opines that the >expression "n&&m*=n--" is incorrect C: >I belive the correctness of a expression is determined by whether or not >the expression can be *generated from* the grammer's production rules; not >whether or not a given implementation of a compiler can parse it. Right. >The given expression is correct C as it can be generated by the following >rules: I didn't much like your particular production. Using the pANS grammar, the conclusion that the parse is NOT effectively "(n&&m) *= n--" is a correct deduction; the key point is that "n&&m" cannot possibly be parsed as anything that is allowed as the left operand of the assignment expression (called a "unary expression" in the pANS grammar). However, the grammar does not support the production you gave, either. At least one set of parentheses would have to be added to obtain a valid expression. The phrase-structure grammar does not in general allow arbitrary expressions as operands, only limited classes of expressions. From omepd!mipos3!intelca!oliveb!ames!mailrus!tut.cis.ohio-state.edu!rutgers!bellcore!texbell!sugar!ficc!peter Wed Mar 15 12:45:45 PST 1989 Article 17612 of comp.lang.c: Path: intelisc!omepd!mipos3!intelca!oliveb!ames!mailrus!tut.cis.ohio-state.edu!rutgers!bellcore!texbell!sugar!ficc!peter >From: peter@ficc.uu.net (Peter da Silva) Newsgroups: comp.lang.c Subject: Re: programming puzzle (silly) Message-ID: <3412@ficc.uu.net> Date: 14 Mar 89 19:27:00 GMT References: <28336@ucbvax.BERKELEY.EDU> <2821@goofy.megatest.UUCP> <2273@buengc.BU.EDU> Organization: Xenix Support Lines: 13 >From klier@ucbarpa.Berkeley.EDU (Pete Klier): > main(m,n){scanf("%d",&n);for(m=n>0^n>9;n&&m*=n--;); > printf(m?"Answer=%d\n":"error\n",m);} Give up one character and it works: main(m,n){scanf("%d",&n);for(m=n>0^n>9;n?m*=n--:0;); printf(m?"Answer=%d\n":"error\n",m);} -- Peter da Silva, Xenix Support, Ferranti International Controls Corporation. Business: uunet.uu.net!ficc!peter, peter@ficc.uu.net, +1 713 274 5180. Personal: ...!texbell!sugar!peter, peter@sugar.hackercorp.com. Article 5989 of comp.misc: Path: intelisc!littlei!ogccse!blake!uw-beaver!cornell!mailrus!uflorida!haven!aplcen!bink From: bink@aplcen.apl.jhu.edu (Ubben Greg) Newsgroups: comp.misc Subject: Re: programming challenge (really silly) Keywords: dc factorial Message-ID: <881@aplcen.apl.jhu.edu> Date: 17 Mar 89 03:44:34 GMT References: <2102@jasper.UUCP> <93227@sun.uucp> Reply-To: bink@aplcen.apl.jhu.edu (Greg Ubben 301-768-9780) Distribution: na Organization: DoD Lines: 21 Posted: Thu Mar 16 19:44:34 1989 In article <93227@sun.uucp> landauer@sun.UUCP (Doug Landauer) writes: >Anyway, I haven't seen any other postings that solve this silly >challenge (to write a little factorial program) in as few characters as >does this 76-character shell/dc/sed script: >---- >dc<[[error]pq]se`head -1`dsnd1>ed9---- >Can anyone improve this further? Being somewhat of a {sh,sed,dc,awk,etc} fanatic, I couldn't resist playing with Doug's script, coming up with the following version which is only 53 characters long (if you remove the trailing newline): ?[[error]pq]sad1>ad9 Date: 19 Mar 89 01:45:00 GMT References: <817@krafla.rhi.hi.is> Lines: 26 Nf-ID: #R:krafla.rhi.hi.is:817:p.cs.uiuc.edu:79700028:000:959 Nf-From: p.cs.uiuc.edu!gillies Mar 18 19:45:00 1989 Here are some memorable problems from some high school programming contests ten years ago. About 1/5 of the entrants solved 7 problems (all of them) in 3 hours: 1. Compute 2^60 EXACTLY and print out the result (note: this can be done cleverly using two or three 32-bit words, and manually shifting the overflow to the second or third variable) 2. Compute 1/(2^60) and print out the result. [Next year's problem] 3. Define a "perfect card deck shuffle". Assume the cards are numbered 1..52. Where is 13 after 2087 (or some huge number) of "perfect shuffles". 4. Write a program to translate integers to roman numerals, and print them (judge tests the program interactively) 5. Compute the number of ways to make change for $1, $2, and/or $5, using (whatever assumptions you want about legality of dollar bills). If you have young contestants, remember to include a few "easy" problems so nobody goes home unhappy. Good luck with your contest!