0

I am trying to create a method that is tail recursive and finds the sum of an equation (i / 2i + 1) where i needs to increment 1-10. I'm having trouble with how to reach the base case and make the recursion cease.

This is what I have so far:

public class SumSeries {

    public static void main(String[] args) {
        System.out.println(sumSeries());
    }

    public static double sumSeries(){
        int i = 10;

        if (i == 0)
            return 0;
        else
            return (i / (2 * i + 1));
    }
}
blackbishop
  • 30,945
  • 11
  • 55
  • 76
Monocyte
  • 95
  • 7
  • 6
    Well, your method does not contain a **recursion**. Recursion means: calling method A from within method A. But you are not calling **sumSeries()** within the body of your method?! Could it be that your else statement should read like `return sumSeries(i ...` – GhostCat Apr 09 '15 at 11:52
  • http://stackoverflow.com/questions/3616483/why-does-the-jvm-still-not-support-tail-call-optimization – Paweł Adamski Apr 09 '15 at 11:53
  • 1
    I'm not sure what you're asking. Is the equation something like (1 / 2*1 + 1) + (2 / 2*2 + 1) + (3 / 2*3 + 1) + ... ? Also, as others have pointed out, recursion means a function is calling itself. Your code does not do that. – futureelite7 Apr 09 '15 at 11:56
  • @futureelite7 Yes, that's how the equation should work. – Monocyte Apr 09 '15 at 12:06

4 Answers4

2

I think that you are looking something like that:

public class SumSeries {

    public static void main(String[] args) {
        System.out.println(sumSeries(10,0));
    }

    public static double sumSeries(int i,double result){

        if (i == 1)
            return result;
        else{
          double res = result + (i / (double)(2 * i + 1));
          return sumSeries(i-1,res);
       }
   }
}
Thanos Pappas
  • 167
  • 1
  • 1
  • 9
1

If you want recursion, your method should look something like this:

public class SumSeries {

    public static void main(String[] args) {
        System.out.println(sumSeries());
    }

    // if you want to keep the argument-less method in main but want to calculate
    // the sum from 1 - 10 nevertheless.
    public static double sumSeries() {
      return sumSeries(10);
    }

    public static double sumSeries(int i){
        if (i == 0) {
            return 0;
        }
        else {
            // The cast to double is necessary. 
            // Else you will do an int-division here and get 0.0 as result.
            // Note the invocation of sumSeries here inside sumSeries.
            return ((double)i / (2 * i + 1)) + sumSeries(i-1);
        }
    }
}
QBrute
  • 4,405
  • 6
  • 34
  • 40
  • This answer is close, but when I ran the problem in an online calculator the sum was 6.066... but with this code the sum is 4.40 because sumSeries(i-1) is being subtracted from the sum. How do I keep that code, so I can reach the base case, but remove it from being summed with the equation? – Monocyte Apr 09 '15 at 12:33
  • 1
    The sum of 6.066 is not correct. See [here:](https://www.wolframalpha.com/input/?i=sum+%28i%2F%282i%2B1%29%29+for+i+%3D+1+to+10). And i did not subtract anything in this method. I just sum all elements from 10 to 1. It's just the reverse order :) – QBrute Apr 09 '15 at 12:38
  • oh, thank you. i used that exact site and got the sum of 6.066. user error. :) – Monocyte Apr 09 '15 at 12:40
0

If you're looking for a way to find this sum :

(1 / 2*1 + 1) + (2 / 2*2 + 1) + (3 / 2*3 + 1) + ... + i/(2i + 1)

You could try this :

public double sumSeries(int i) {
   if (i == 1) { // base case is 1 not 0
     return 1/3;
   } else {
     double s = i / (2.0 * i + 1.0);
     return s + sumSeries(i - 1);
   }
} 
blackbishop
  • 30,945
  • 11
  • 55
  • 76
0

Your method is not recursive. A recursive method needs to use 'himself' and at a certain condition it stops. An example:

public static double sumSeries(int x) { 
    if (x == 0)   
        return x;
    else {  
        return x + sumSeries(x - 1);
}

for your example, something like this would fit:

public static double sumSeries(double x) {
    if (x == 0)   
        return x;
    else  
        return (x / (2 * x + 1)) + sumSeries(x - 1.0); 
}

If I understood your algorithm correctly :) If not, edit the algorithm :)

Dopdop
  • 37
  • 3