Path: csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.programming Subject: Re: A little puzzle. Date: Mon, 28 Nov 2022 07:30:11 -0800 Organization: A noiseless patient Spider Lines: 48 Message-ID: <86pmd76qrg.fsf@linuxsc.com> References: <875yf8nijb.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="8f857c192f6688ee1cdff50f6ccf3164"; logging-data="2105498"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ctcSs8ah3nlhE3xINPApGZ8eGuWhyYWo=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:pImWBnTxGqJKj1RMFAv0NTWxQCQ= sha1:CfxA9wI7K9bvyRMDKgJJY12dOQs= Xref: csiph.com comp.programming:15983 Richard Harnden writes: > On 21/11/2022 20:45, Ben Bacarisse wrote: > >> I wonder if there are any real posters here? Let's see... >> >> I came across a trivial programming task that must have been solved a >> thousand times by other programmers, but it had never crossed my path >> until yesterday. I must be feeling my age because I made a real hash of >> tackling it at first. Anyway, I thought it might be of interest. >> >> Consider any ordered measure that "wraps round" -- bearings in degrees, >> minutes in the hour, indeed hours in either the 12 or 24 hour clock. >> The problem is to determine if a given value is in the sub-range >> specified by a start and an en value. >> >> I was specifically concerned with integer values where the sub-range >> includes the start value but excludes the end value. >> >> Though I am not sure this merits the term "puzzle", I suggest that >> solutions be posted with some spoiler protection. Do all the news >> readers used by programmers (or ex programmers) all respect the presence >> of a form-feed character... >> >> ... like this? Because that's my favourite way, rather than posting >> lots of dummy lines before the solution. > > I think this works ... > > int in_subrange(int range, int start, int end, int check) > { > check %= range; > > if ( ( end < start && ( > (check >= 0 && check <= end) > || (check >= start && check < range) > ) > ) > || ( check >= start && check <= end ) > ) > return 1; > > return 0; > } Have you thought about a case where the values of check, start, and end are chosen from the interval -9000000000 to 9000000000? How about the interval -18000000000 to 18000000000?