sum of the first n odd positive integers

Discussion in 'C++' started by mathon@gmx.at, Nov 5, 2006.

  1. Guest

    hi,

    i have the following recursive function:

    Code:
    unsigned int sum_odds(unsigned int n)
    {
           if(n==1)
             return 1;
           else
             return sum_odds(n-1)+(2*n -1);
    }
    
    Does anybody know what the result is for the input of 10? - the problem
    is i do not exactly know how i should handle the +(2*n-1)...:(

    matti
     
    , Nov 5, 2006
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    wrote:

    > hi,
    >
    > i have the following recursive function:
    >
    >
    Code:
    > unsigned int sum_odds(unsigned int n)
    > {
    >        if(n==1)
    >          return 1;
    >        else
    >          return sum_odds(n-1)+(2*n -1);
    > }
    > 
    >
    > Does anybody know what the result is for the input of 10?


    Should be 100:

    a ab abc abcd
    bb bbc bbcd
    ccc cccd
    dddd
    1 = 1
    4 = 1+3
    9 = 1+3+5
    16 = 1+3+5+7
    25 = 1+3+5+7+9
    ...

    > - the problem is i do not exactly know how i should handle the

    +(2*n-1)...:(

    What about it?


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Nov 5, 2006
    #2
    1. Advertising

  3. Guest

    the problem is i do not understand when i should add the + (2*n -1) to
    the return values...?


    Kai-Uwe Bux wrote:
    > wrote:
    >
    > > hi,
    > >
    > > i have the following recursive function:
    > >
    > >
    Code:
    > > unsigned int sum_odds(unsigned int n)
    > > {
    > >        if(n==1)
    > >          return 1;
    > >        else
    > >          return sum_odds(n-1)+(2*n -1);
    > > }
    > > 
    > >
    > > Does anybody know what the result is for the input of 10?

    >
    > Should be 100:
    >
    > a ab abc abcd
    > bb bbc bbcd
    > ccc cccd
    > dddd
    > 1 = 1
    > 4 = 1+3
    > 9 = 1+3+5
    > 16 = 1+3+5+7
    > 25 = 1+3+5+7+9
    > ...
    >
    > > - the problem is i do not exactly know how i should handle the

    > +(2*n-1)...:(
    >
    > What about it?
    >
    >
    > Best
    >
    > Kai-Uwe Bux
     
    , Nov 5, 2006
    #3
  4. arnuld Guest

    wrote:
    > hi,
    >
    > i have the following recursive function:
    >
    >
    Code:
    > unsigned int sum_odds(unsigned int n)
    > {
    >        if(n==1)
    >          return 1;
    >        else
    >          return sum_odds(n-1)+(2*n -1);
    > }
    > 


    it works fine without any trouble.

    > Does anybody know what the result is for the input of 10?


    (sorry, but i have written /sum/ in Lisp style to make it concise)

    the sum of first:

    2 +ve integers = 4 (+ 1 3)
    3 +ve integers = 9 (+ 1 3 5)
    4 +ve integers = 16 (+ 1 3 5 7)
    5 +ve integers = 25 (+ 1 3 5 7 9)
    ......................................................
    .......................................................
    10 +ve integers = 100 (+ 1 3 5 7 9 11 13 15 17 19)



    > - the problem
    > is i do not exactly know how i should handle the +(2*n-1)...:(


    when unsure always use parenthesis. in your case, i find it better like
    this:

    return (sum_odds(n-1) + (2*n -1))

    it makes clearer presentation to me.

    thanks


    > matti
     
    arnuld, Nov 5, 2006
    #4
  5. Guest

    hmm...but when i go through the recursion with for example sum_odds(4)

    > >
    Code:
    > > unsigned int sum_odds(unsigned int n)
    > > {
    > >        if(n==1)
    > >          return 1;
    > >        else
    > >          return (sum_odds(n-1) + (2*n -1))
    > > }
    > > 


    i got the following:

    sum_odds(4)

    return (sum_odds(3) + (7)); return 9+7 = 16;

    return (sum_odds(2) + (5)); -> return 4+5 = 9;

    return (sum_odds(1) + (3)); -> return 1+3 = 4;

    return 1

    so when i use 4 i got as result 16...??
     
    , Nov 5, 2006
    #5
  6. Greg Guest

    wrote:
    > hi,
    >
    > i have the following recursive function:
    >
    >
    Code:
    > unsigned int sum_odds(unsigned int n)
    > {
    >        if(n==1)
    >          return 1;
    >        else
    >          return sum_odds(n-1)+(2*n -1);
    > }
    > 
    >
    > Does anybody know what the result is for the input of 10? - the problem
    > is i do not exactly know how i should handle the +(2*n-1)...:(


    How about a non-recursive implementation:

    unsigned sum_odds(unsigned n)
    {
    return n * n;
    }

    Should be correct as long as n * n does not overflow.

    Greg
     
    Greg, Nov 5, 2006
    #6
  7. Mark P Guest

    wrote:
    > hi,
    >
    > i have the following recursive function:
    >
    >
    Code:
    > unsigned int sum_odds(unsigned int n)
    > {
    >        if(n==1)
    >          return 1;
    >        else
    >          return sum_odds(n-1)+(2*n -1);
    > }
    > 
    >


    Even though I rather doubt that this code is being used for any
    production software, you should nonetheless get in the habit of looking
    for potentially bad inputs. What happens if I call your function on 0?

    > Does anybody know what the result is for the input of 10? - the problem
    > is i do not exactly know how i should handle the +(2*n-1)...:(


    What is it that you think you need to "handle"? Better yet, what is it
    that this function is supposed to actually do?
     
    Mark P, Nov 5, 2006
    #7
  8. arnuld Guest

    > Greg wrote:

    > How about a non-recursive implementation:
    >
    > unsigned sum_odds(unsigned n)
    > {
    > return n * n;
    > }


    WOW! this is great :)
     
    arnuld, Nov 5, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski

    Design-Time Validation for positive integers

    Nathan Sokalski, Oct 20, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    378
    James Hahn
    Oct 21, 2009
  2. Hicham Mouline
    Replies:
    2
    Views:
    777
    Keith Thompson
    Apr 23, 2010
  3. Vincent Davis
    Replies:
    0
    Views:
    97
    Vincent Davis
    Jun 28, 2013
  4. Peter Otten
    Replies:
    0
    Views:
    110
    Peter Otten
    Jun 28, 2013
  5. Joshua Landau
    Replies:
    0
    Views:
    95
    Joshua Landau
    Jun 28, 2013
Loading...

Share This Page