Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #105720

Re: Program to find consecutive integers

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Program to find consecutive integers
Date 2017-03-10 10:38 +0000
Organization A noiseless patient Spider
Message-ID <87zigtxvpw.fsf@bsb.me.uk> (permalink)
References <o9t9jl$bkg$1@gioia.aioe.org> <1bpohpg0sy.fsf@pfeifferfamily.net> <o9ti7j$lnp$1@gioia.aioe.org> <slrn3vfsoc4oes.lda.ike@iceland.freeshell.org> <o9tp9d$10e0$1@gioia.aioe.org>

Show all headers | View raw


insane_goat <nobody@nothing.net> writes:

> On 2017-03-10, Ike Naar <ike@iceland.freeshell.org> wrote:
>> On 2017-03-10, insane_goat <nobody@nothing.net> wrote:
>>> On 2017-03-10, Joe Pfeiffer <pfeiffer@cs.nmsu.edu> wrote:
>>>> Umm.... you're going to have to give a more complete description of the
>>>> problem statement before I, at any rate, will want to think about your
>>>> program.
>>>>
>>>> To me, producing consecutive integers boils down to
>>>>
>>>> for (i = start; i <= end; i++)
>>>>     printf("%\n", i);
>>>     
>>> Sorry. I was in a bit of a rush.
>>>
>>> I meant consecutive integers which add up to the target.
>>
>> The following sets of consecutive integers add up to the target 15:
>> {1,2,3,4,5}
>> {4,5,6}
>> {7,8}
>> {15}
>>
>> Your algorithm produces the set {1,2,3,4,5},
>> Joe's algorithm produces the set {15}.
>>
>> Why is your solution better than Joe's ?
>
> Because it can find consecutive integers adding up to e.g. 296, which cannot
> be produced by starting from 1.

296 is the sum of the sequence (296).  The full statement of your
problem will either include some restrictions (for example the sequence
must be longer than 1 or the largest number in it must be less than some
limit) or some criterion for the best solution (the longest, the
shortest, etc) or maybe the requirement to produce (or count) all the
solutions.  You are probably working from some external problem
statement.  Can you link to it?

One thing to note is that your code allocated storage and did a lot of
adding things up (sorry, I did not study it) but you'll find a little
bit of mathematics goes a long way in this case.

Yours is not really a C question, but a C-related question is why are
you using C?  (Not a trick question, I'm genuinely interested.)

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Program to find consecutive integers insane_goat <nobody@nothing.net> - 2017-03-10 04:21 +0000
  Re: Program to find consecutive integers Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-03-09 22:26 -0700
    Re: Program to find consecutive integers insane_goat <nobody@nothing.net> - 2017-03-10 06:48 +0000
      Re: Program to find consecutive integers Ike Naar <ike@iceland.freeshell.org> - 2017-03-10 08:18 +0000
        Re: Program to find consecutive integers insane_goat <nobody@nothing.net> - 2017-03-10 08:48 +0000
          Re: Program to find consecutive integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-10 10:38 +0000
            Re: Program to find consecutive integers insane_goat <nobody@nothing.net> - 2017-03-12 13:15 +0000
              Re: Program to find consecutive integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-12 13:29 +0000
            Re: Program to find consecutive integers raltbos@xs4all.nl (Richard Bos) - 2017-03-14 07:49 +0000
          Re: Program to find consecutive integers Öö Tiib <ootiib@hot.ee> - 2017-03-10 05:12 -0800
        Re: Program to find consecutive integers jameskuyper@verizon.net - 2017-03-10 07:16 -0800
          Re: Program to find consecutive integers Ike Naar <ike@iceland.freeshell.org> - 2017-03-10 19:20 +0000
            Re: Program to find consecutive integers James Kuyper <jameskuyper@verizon.net> - 2017-03-10 15:21 -0500
              Re: Program to find consecutive integers Ike Naar <ike@iceland.freeshell.org> - 2017-03-10 20:36 +0000
              Re: Program to find consecutive integers insane_goat <nobody@nothing.net> - 2017-03-12 13:19 +0000
                Gotta keep up with the times (Was: Program to find consecutive integers) gazelle@shell.xmission.com (Kenny McCormack) - 2017-03-12 13:25 +0000
                Re: Gotta keep up with the times (Was: Program to find consecutive integers) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-12 08:11 -0700
                Re: Gotta keep up with the times (Was: Program to find consecutive integers) gazelle@shell.xmission.com (Kenny McCormack) - 2017-03-12 15:53 +0000
                Re: Gotta keep up with the times (Was: Program to find consecutive integers) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-12 10:50 -0700
                Re: Gotta keep up with the times (Was: Program to find consecutive integers) gazelle@shell.xmission.com (Kenny McCormack) - 2017-03-12 18:25 +0000
                Re: Gotta keep up with the times (Was: Program to find consecutive integers) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-03-12 16:20 -0700
                Re: Gotta keep up with the times gordonb.g2pqz@burditt.org (Gordon Burditt) - 2017-03-14 01:09 -0500
                Re: Gotta keep up with the times raltbos@xs4all.nl (Richard Bos) - 2017-03-14 07:43 +0000
                Re: Program to find consecutive integers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-03-12 10:28 -0400
            Re: Program to find consecutive integers Gareth Owen <gwowen@gmail.com> - 2017-03-11 07:42 +0000
      Re: Program to find consecutive integers jt@toerring.de (Jens Thoms Toerring) - 2017-03-10 10:21 +0000
        Re: Program to find consecutive integers Nathan Wagner <nw@hydaspes.if.org> - 2017-03-10 22:07 +0000
          Re: Program to find consecutive integers Nathan Wagner <nw@hydaspes.if.org> - 2017-03-10 22:20 +0000
          Re: Program to find consecutive integers jr <creature.eternal@gmail.com> - 2017-03-10 23:49 -0800
        Re: Program to find consecutive integers jt@toerring.de (Jens Thoms Toerring) - 2017-03-11 23:38 +0000
          Re: Program to find consecutive integers Tim Rentsch <txr@alumni.caltech.edu> - 2017-04-16 21:18 -0700

csiph-web