Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sum of the first n odd positive integers

Reply
Thread Tools

sum of the first n odd positive integers

 
 
mathon@gmx.at
Guest
Posts: n/a
 
      11-05-2006
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

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-05-2006
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
 
Reply With Quote
 
 
 
 
mathon@gmx.at
Guest
Posts: n/a
 
      11-05-2006


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


 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      11-05-2006
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


 
Reply With Quote
 
mathon@gmx.at
Guest
Posts: n/a
 
      11-05-2006

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...??

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      11-05-2006

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

 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      11-05-2006
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?
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      11-05-2006
> Greg wrote:

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


WOW! this is great

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
floor(positive double) vs trunc(positive double) different Hicham Mouline C Programming 2 04-23-2010 06:50 PM
Design-Time Validation for positive integers Nathan Sokalski ASP .Net 1 10-21-2009 04:00 AM
Sum of all even integers ericcc Java 3 10-10-2007 02:56 AM
sum of two integers robin C++ 20 09-19-2007 12:47 PM
Odd behavior with odd code Michael Speer C Programming 33 02-18-2007 07:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57