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