Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Sort of trivial code challenge - may be interesting to you anyway Date: Sat, 07 Mar 2026 12:02:11 -0800 Organization: A noiseless patient Spider Lines: 91 Message-ID: <86o6kz9zng.fsf@linuxsc.com> References: <10n80sc$3soe4$1@dont-email.me> <86v7feei2e.fsf@linuxsc.com> <10o53k6$1i0ef$2@dont-email.me> <86ms0peby6.fsf@linuxsc.com> <10ockdh$3qpk6$1@dont-email.me> <10ocrjn$3qpk6$2@dont-email.me> <10od64s$3qpk6$4@dont-email.me> <86ikb9bmtw.fsf@linuxsc.com> <10oem5t$n5hk$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Sat, 07 Mar 2026 20:02:14 +0000 (UTC) Injection-Info: dont-email.me; posting-host="fa47ee131b44f7fa0236e5dadd002153"; logging-data="1894650"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FFictuNao7pPCP7QXuj4MGQQdRp/bhv0=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:bqYc+rZOyodQGhZNnpZ7+5BHXnI= sha1:QcJ2cQAoo3fTF4uda1V6Oh4QOJk= Xref: csiph.com comp.lang.c:396851 Lew Pitcher writes: > [...] I look forward to seeing /your/ code :-) Here we go... #include #include #include #include typedef size_t Z; typedef _Bool B; static Z k, h, w, c; static int d; static jmp_buf jb; #define GO(x) longjmp( jb, x ) #define NUMBERISH(p) ((p) && *(p) && (p)[ strspn((p),"0123456789") ] == 0) enum { START, // the initial state; must be zero AFU, USAGE, // give suitable message and exit ROOT, DRAT, // find ceiling( sqrt( cutoff ) ); possible no joy exit HWC, // display H*W values with cutoff C FIN, // exit program }; int main( int argc, char *argv[] ){ switch( setjmp( jb ) ){ case START: { B usage = argc < 2 || argc > 4; B g1 = argc > 1 && NUMBERISH( argv[1] ); B g2 = argc > 2 && NUMBERISH( argv[2] ); B g3 = argc > 3 && NUMBERISH( argv[3] ); Z a1 = g1 ? strtoul( argv[1], 0, 10 ) : 0; Z a2 = g2 ? strtoul( argv[2], 0, 10 ) : 0; Z a3 = g3 ? strtoul( argv[3], 0, 10 ) : 0; B square = argc == 2 && g1; B hw = argc == 3 && g1&&g2 && a1&&a2; B hwc = argc == 4 && g1&&g2 && a1&&a2 && g3; k = 0; h = hw || hwc ? a1 : a1/4 + !a1; // note: initial value(h) > 0 w = hw || hwc ? a2 : h; c = square ? a1 : !hwc ? h*w : a3 > h*w ? h*w : a3; d = snprintf( 0, 0, "%zu", c ); GO( usage ?USAGE : square ?ROOT : (hw||hwc) && a1&&a2 ?HWC : AFU ); } case AFU: printf( " urk... bad arguments\n" ); GO( USAGE ); case USAGE: printf( " usage:\n" ); printf( " %s cutoff {{for square}}\n", argv[0] ); printf( " %s rows columns [cutoff]\n", argv[0] ); GO( FIN ); case ROOT: { h = c < 2 ? 1 : c < 5 ? 2 : (h + c/h) / 2; h += h*h < c; w = h; B done = c < 2 || h*h < c+2*h && c <= h*h; B good = c < 2 || h*h < c+h && c <= h*h; GO( !done ?ROOT : good ?HWC : DRAT ); } case DRAT: printf( " square with cutoff %zu - no joy\n", c ); GO( FIN ); case HWC: { B eol = k/h + 1 == w; B end = k+1 + (h>c ? h-c : 0) >= h*w; Z nextk = eol ? k - (w-1)*h + 1 : k+h; printf( k < c ? " %*zu" : "", d, k+1 ); printf( c > 0 && eol ? "\n" : "" ); GO( end ?(printf( "------\n" ), FIN) : (k = nextk, HWC) ); } } }