0

I need to use recursion to make an algorithm which finds the sum of x integers after an integer m. For example, if m is 2 and n is 5, I would need to find the sum of 2+3+4+5+6 using recursion.

The code I have so far would (for the example illustrated above) works in such way: 2+3+3+3+3. Any help at all is greatly appreciated as I have an exam tomorrow and questions like these may be included.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Input a value for n");
    int n = input.nextInt();
    System.out.println("Input a value for m");
    int m = input.nextInt();

    System.out.println(sho (m,n));

}
public static int sho (int m, int n){

    int sum = 0;
    if(n<=0){
        return m;
    }

     sum = sho(m+1,n-1);

    sum = sum +(m);
    return sum;

}
6
  • Your base case (when n == 0) should return 0 instead of m but otherwise it looks OK. What makes you think it has the problem you described? Commented Jan 25, 2018 at 5:02
  • because it does not work properly when i run it. Also, I printed m each time of recursion and found that m stays the same, so the value being added to the initial integer does not change. Commented Jan 25, 2018 at 5:04
  • Have you tried to Google a similar problem? Take a look at this question and try to add the lower limit. Commented Jan 25, 2018 at 5:07
  • @FaizanRiaz I cannot reproduce this. works perfectly with the change I have proposed. Commented Jan 25, 2018 at 5:09
  • @Henry thank you so much it works perfectly ! Commented Jan 25, 2018 at 5:12

2 Answers 2

0

You only need to implement the summation of a series using recursion. The starting and ending points are really just minor logic. The inductive case of the recursion is is that the current value is less than the end of the series. In this case, we return the current value plus a recursive call with the starting point increased by one. The base case occurs when we reach the end of the series. For the base case, we just return the max value and stop the recursion.

public static int sho(int curr, int max) {
    if (curr == max) {
        return curr;
    }
    else {
        return curr + sho(curr + 1, max);
    }
}

public static void main(String args[]) {
    System.out.println(sho(10, 15)); // 10 11 12 13 14 15
}

One problem I see with your code is your recursion logic:

sum = sho(m+1, n-1);

I don't think that decreasing the upper bound is the way to go here. Instead, keep that bound fixed and advance through the series by making another recursive call. Decreasing the upper bound sounds like mixing recursion with an iterative approach.

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

The second parameter is not the upper bound, it is the number of items to add.
@Henry No, it is the upper bound of the range. Check my logic. I call sho(10, 15), and it generates the sum from 10 to 15 inclusive. The number of items to add is implicit within the recursion (technically it is the number of frames on the call stack).
Not in your progam, in OP's original version. Therefore sum = sho(m+1, n-1); is correct.
0
public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Input a value for n");
        int n = input.nextInt();
        System.out.println("Input a value for m");
        int m = input.nextInt();

        System.out.println(sho (m,n,0));

    }
    public static int sho (int m, int n,int sum){


        if(n<=0){
            return sum;
        }

        sum=sum+m;
         sum = sho(m+1,n-1,sum);

        return sum;

    }

Try this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.