Results 1 to 4 of 4
 04112011, 04:18 AM #1Member
 Join Date
 Nov 2010
 Posts
 9
 Rep Power
 0
Having trouble tracing this recursion method. Can't find the problem.
I'm working on an assignment and I'm just stumped. I'm getting a stack overflow so I think I'm sending it off to infinity somewhere but I can't for the life of me figure out where. Can anyone see the problem with it?
The assignment was,
Modify the method that calculates the sum of the integers between 1 and N. Have the new version match the following recursive definition: The sum of 1 to N is the sum of 1 to (N/2) plus the sum of (N/2+1) to N.
The original method looks like this:
Java Code:public int sum (int num) { int result; if (num == 1) result = 1; else result = num + sum (num1); return result; }
Java Code:import java.util.Scanner; public class SumDiv2 { public static int sum(int num, int stop){ int result; if(num == stop) result = stop; else result = (num + sum(num1, (num+stop)/2+1)) + ((num+stop)/2 + sum((num+stop)/21, stop)); return result; } public static void main(String args[]){ Scanner s = new Scanner(System.in); int n = 0; System.out.println("Enter an ingeger greater than 0: "); n = s.nextInt(); System.out.println("The sum of the " + n + " integers is " + sum(n)); } public static int sum(int num){ return sum(num, 0); } }
 04112011, 04:55 AM #2
Your logic is wrong in the sum(int,int) method. Try looking over it again. I suggest starting with a simple number like 3 and actually draw it out on a piece of paper what the method calls and values are.
 04112011, 04:55 AM #3
Why have you introduced "stop" and a second method?
 04122011, 06:44 AM #4Member
 Join Date
 Nov 2010
 Posts
 9
 Rep Power
 0
I could have simply made a direct call from the main to the sum(int, int) but I was trying to remain as close to the original as possible so I included the interim step. Not sure if you were asking why the additional int is needed but if so, it's to tell the method which section to work on summing. Otherwise, both sides would sum all numbers up to 0, leading to a larger end result than the correct answer.
I ended up with
Java Code:public static int sum(int start, int stop){ int result; if(start == stop) result = start; else if(start < stop) result = 0; else{ result = start + sum(start1, ((start+stop)/2)+1); result += (start+stop)/2 + sum(((start+stop)/2)1, stop); } return result; }
This is beyond the assignment but I'm just curious.
Similar Threads

Turning Recursion Method into Iterative method
By mattakuevan in forum New To JavaReplies: 9Last Post: 06152010, 06:46 AM 
stack overflow error, i want to call a method within itself not using recursion tho
By bigboi26 in forum New To JavaReplies: 1Last Post: 03172010, 06:25 AM 
My first recursion method: the number e
By Lil_Aziz1 in forum Reviews / AdvertisingReplies: 1Last Post: 01232010, 11:28 PM 
implement binary search method using recursion?
By chopo1980 in forum New To JavaReplies: 1Last Post: 12122009, 04:58 PM 
Trouble with method
By BlueJ2008 in forum New To JavaReplies: 2Last Post: 10192008, 09:05 PM
Bookmarks