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: Thu, 05 Mar 2026 15:05:09 -0800
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <86v7f9c1y2.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> <86zf4mapto.fsf@linuxsc.com> <10ocvrk$3qpk6$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Thu, 05 Mar 2026 23:05:12 +0000 (UTC)
Injection-Info: dont-email.me; posting-host="e10200766ffc466ebfe6a0b9c1907eec"; logging-data="229228"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19X6/fLywM8ZZLYzQG0QO4ltOrrJU//dqo="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:r8bmegdRSfU1FcaokRHgl8YqE4I= sha1:I+eF0II30n3ZCBqGFqtSINvSWmw=
Xref: csiph.com comp.lang.c:396802
Lew Pitcher writes:
> On Thu, 05 Mar 2026 14:12:19 -0800, Tim Rentsch wrote:
>
>> Lew Pitcher writes:
>>
>>> On Thu, 05 Mar 2026 19:09:37 +0000, Lew Pitcher wrote:
>>>
>>>> On Mon, 02 Mar 2026 21:09:21 -0800, Tim Rentsch wrote:
>>>> [snip]
>>>>
>>>>> The latest challenge, which I just got through doing, is to
>>>>> disallow if, for, while, goto, return, and to forbid functions
>>>>> and function calls except for calls to C standard library
>>>>> functions. Also no math library. :)
>>>>
>>>> Inventive, aren't you :-)
>>>>
>>>> I've got a working matrix print that (I think) satisfies your
>>>> requirements, but have not started on the argument processing
>>>> logic yet. I may, yet again, revise my approach, as the solution
>>>> I'm using is quite tedious to code.
>>>
>>> OK, so the "no if statements" is a bit of a bother, but not
>>> insurmountable. It's just a case of switching things around.
>>> And, perhaps there's a third option as well.
>>>
>>> As I said, the alternatives are just tedious to code.
>>
>> I did manage to find some tricks to make things simpler, but
>> probably the most important is ?: is your friend.
>
> that, and switch()/case, which is a nice substitute for if () statements
> with complex bodies, or if () / else statements.
>
>>> But, I now need to find a replacement for sqrt() that doesn't take
>>> a lot of space. Back to first principles for me, then.
>>
>> There is an easy way to do this, if not especially elegant: start a
>> counter at 0 and count it up until counter*counter >= cutoff. Then
>> there is a straightword arithmetic test to see if counter is good or
>> bad.
>
> I was playing with the Heron's method of approximation, but was
> getting anomalous numbers when coded to the restrictions of the
> challenge.
>
> I don't mind burning cycles, and might go for the straightforward
> way, but I'm still thinking on it
The simple counter method is what I first coded. Then I did a
binary search. The binary search is kind of clunky in a
"no for() or while()" environment, so I figured out a way
to use Newton-Raphson iteration. A little bit tricky to get
right, but after wrestling with it I managed to get a fairly
nice formulation.