# Recursive sum?

Page 1 of 1

## 5 Replies - 21749 Views - Last Post: 30 July 2008 - 03:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=58988&amp;s=c6af4efef6ae0d35750566297f43c1ed&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 zukker

• New D.I.C Head

Reputation: 5
• Posts: 22
• Joined: 10-July 08

# Recursive sum?

Posted 29 July 2008 - 03:50 AM

Don't know if this is the right section but here goes. I'm having trouble understanding this assignment, I hope someone could shed some light. I'm not expecting anyone doing this for me, just help me understand what I should do.

I'll try to explain is as well as I can.

a recursive method that calcuates the sum of 1+2+3+4+...+N. The process should occur like this: sum 1->N equals sum 1-to-(N/2) plus the sum of (N/2+1)-to-N

something like:

sum(0,10)
sum(0,5) + sum(6,10)
(0,2)+(3,5) + (6,8)+(9,10)
etc.

```public sum (int n) {

if(n == 1)
return 1;
else
return sum(n/2)+sum(n/2+1)

```

this is just how I understand it.

Is This A Good Question/Topic? 0

## Replies To: Recursive sum?

### #2 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## Re: Recursive sum?

Posted 29 July 2008 - 04:27 AM

Well, look what you have written under something like and your code, do they match? it's just that how you implement the steps you have got.

This is what I can think of right now...

```public sum (int from, int upto)
{

if(from == upto) //If we have reached to a single number to add then return that number say the call was sum(5,5)
return upto;
else
return sum(from,((from+upto)/2))+sum((((from+upto)/2)+1),upto);  // so we are splitting your sum(0,10) to sum(0,5)+sum(6,10) here.
//why (from+upto)/2? think of the case where call is sum(3,5) in this case if we do upto/2 the call
//we make inside will be sum(3,2)+sum(2,5)!!! so we have to do it as (from+upto)/2 which will
//make the call as sum(3,4)+sum(5,5).
}

```

--EDIT--

This post has been edited by AmitTheInfinity: 29 July 2008 - 04:32 AM

### #3 zukker

• New D.I.C Head

Reputation: 5
• Posts: 22
• Joined: 10-July 08

## Re: Recursive sum?

Posted 29 July 2008 - 05:49 AM

Thanks alot That made it alot clearer for me atleast

### #4 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Recursive sum?

Posted 29 July 2008 - 08:03 AM

Seems like a much harder way to do a sum with recursion...

```public int sum(int num, int limit)
{
if (num == limit)
return limit;

else
return sum((num + 1), limit);
}
```

And you also did not declare a return type for your method, int should go between public and sum.

This post has been edited by Locke37: 29 July 2008 - 08:06 AM

### #5 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## Re: Recursive sum?

Posted 29 July 2008 - 11:54 PM

aaaah, how did I forgot the return type!!! Thanks for pointing that out Locke37.

And yes, it was the very ugly way to do sum with recursion. But I think their teacher wanted them to do it that way, just because he must be interested in showing them some complex things. [though they are with the simple functionality ]

### #6 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Recursive sum?

Posted 30 July 2008 - 03:35 PM

AmitTheInfinity, on 29 Jul, 2008 - 11:54 PM, said:

aaaah, how did I forgot the return type!!! Thanks for pointing that out Locke37.

And yes, it was the very ugly way to do sum with recursion. But I think their teacher wanted them to do it that way, just because he must be interested in showing them some complex things. [though they are with the simple functionality ]

This post has been edited by Locke37: 30 July 2008 - 03:36 PM