programming challenge (really silly)

Doug Landauer landauer at morocco.Sun.COM
Fri Mar 10 12:00:08 AEST 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<<Q
[[error]pq]se`head -1`dsnd1>ed9<e[ln1-dsn*ln2<y]syln2<y[answer is ]Pp
----
(Those lacking the BSD "head" program can use "cat" instead (shortening
the solution to 72 chars) but you'll have to give a ^D after you input
the number.)

Can anyone improve this further?   I suspect it can be done more concisely in
APL, but I have no APL interpreter handy.

Meanwhile, I later came up with this cute 62-character near-solution,
that doesn't need anything but a shell (vanilla Bourne, I think).
However, it doesn't print "error" in all of the cases that it should:
----
read x
set 1 2 6 24 120 720 5040 40320 362880
eval echo $"$x"
----
Call this one "error" (and set your $PATH right) and it'll mimic the
original for 0 through 9!

A related (but shorter -- 54 characters) near-solution does almost as
well, but won't work on some BSD-based systems lacking "cut" (You should
replace each "^I" with an actual TAB character):
---
read x
cut -f$x<<Q
1^I2^I6^I24^I120^I720^I5040^I40320^I362880
---

(P.S. In case you care, most of these were tested on a Sun4 running 4.0).

Oh well, back to work.
--
  Doug Landauer -- Sun Microsystems, Inc.	Software Products Group
  landauer at sun.com   or   ...!sun!landauer



More information about the Comp.lang.c mailing list