5 Replies - 15443 Views - Last Post: 30 July 2008 - 03:35 PM Rate Topic: -----

#1 zukker  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • 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  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • 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).
}



I hope this will help you... :)

--EDIT--
Added comments in code.

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

Was This Post Helpful? 1
  • +
  • -

#3 zukker  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • 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 :^:
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Recursive sum?

Posted 29 July 2008 - 11:54 PM

aaaah, how did I forgot the return type!!! :crazy: 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 :) ]
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Recursive sum?

Posted 30 July 2008 - 03:35 PM

View PostAmitTheInfinity, on 29 Jul, 2008 - 11:54 PM, said:

aaaah, how did I forgot the return type!!! :crazy: 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1