Hi there,

I wrote a VERY simply program attempting to use a recursive function to calculate a math problem and spit it out onto a table.

It prints once and then I'm getting a stack oveflow error on line 8 and then line 12:

package recurtable;

import java.text.*;
        
public class RecurTable {
    static int D(int n) 
    {
        if (n == 10 ) 
        {
          return 10;
        }
        return (D(n - 10) + n);
    }
    
    public static void main(String[] args) {
        
      System.out.println("\n n\t D(n))\t D(n)/(n*n)");
      
      for (int n = 20; n <= 250; n++) 
      {
          int d_val = D(n);

          DecimalFormat df = new DecimalFormat("#.####");
          
          System.out.println(n + "\t" + d_val + "\t" + 
                  df.format(d_val/(n*n)));
      }
    }
}

I'm new to recursion, so I'm unsure what's wrong with the function. Thanks guys!

the stackoverflow happens because of an infinite recursion

the value 10 is not the base value, you need to have a condition for the values less than 10 or at least less than or equal to 0

for example D(21) will result to D(9)+10 then (D(-1)+10)+10 and so on

Edited 4 Years Ago by zeroliken: n/a

Well, the base case is supposed to be that D(10) = 10.

I don't understand how D(21) would equal D(9) + 10? I look at it like this:

D(21) = D(21 - 10) + 21
then
D(11) + 21
then
D(-1) + 21
...

I know that is wrong, but why?

I don't truly understand how recursion is supposed to work, I was just trying it out with a random equation.

Thanks for your help!

whoops didn't notice my math error, sorry about the example that's not suppose to be D(9) but D(11) :$

anyway about the recursion you need a base case where the recursion will eventually lead to for all values passed on to it,
in your code if the value of int doesn't equal to 10 it will keep on running, The + n won't apply yet until a value is returned for D(), maybe make a base value where all numbers will eventually lead to like if it becomes less than or equal to 0

Edited 4 Years Ago by zeroliken: n/a

Ah, okay gotcha. I'll continue to look into this keeping what you said in mind! :D

I'll leave this unsolved for now, though, until I'm sure I get it right.

Comments
good luck :)

Remember, with recursion, just as with loops, you need a terminating condition that will ALWAYS be reached (unless you want an endless loop), which we sometimes call a barrier condition. You need to test for when you reach a value less than 10, so change your code for D(int) to something like this:

static int D(int n) 
    {
        if (n < 10)
        {
            return 0;
        }
        return (D(n - 10) + n);
    }

Remember, with recursion, just as with loops, you need a terminating condition that will ALWAYS be reached (unless you want an endless loop), which we sometimes call a barrier condition. You need to test for when you reach a value less than 10, so change your code for D(int) to something like this:

static int D(int n) 
    {
        if (n < 10)
        {
            return 0;
        }
        return (D(n - 10) + n);
    }

I found the main issue that I was having, but I'm not so sure how to solve it.

The number sequence that the for loop is stepping through should not be from 20 to 250 (implying that it will follow a sequence like this: 20,21,22,23,24,...), it should be following a sequence like this:

10,20,30,40,50,60,...,250


So I'm guessing if I add something like:

int k = n*10;
int d_val = D(k);

in the for loop and change the range to this:

for (int n = 2; n <= 25; n++)

It should be good as it's counting by tens (10,20,30,etc).

Edited 4 Years Ago by Syrne: n/a

This question has already been answered. Start a new discussion instead.