-4

I am having a little issue with my code. I am trying to find the runtime to compare to some math that I have prepared for the problem. I have one method in particular that I am testing that goes like this:

 public static int foo(int n, int k){
        long startTime = System.nanoTime();
        if(n<=k){
            long endTime = System.nanoTime();
            System.out.println("checkFoo");
            System.out.println("start time: " +startTime);
            System.out.println("end time: " +endTime);
            return 1;
        }
        else{
            return foo(n/k,k) + 1;
        }
    }

I test this code in my main method in the following way:

public static void main(String[] args){
    foo(1, 1);
    foo(5, 1); 
    foo(10, 1);
    foo(100, 1);
}

I get an error where it says

Exception in thread "main" java.lang.StackOverflowError

and then it repeats the line:

at Problem3.foo(Problem3.java:42) 

I am wondering if this has to do with the fact that foo is supposed to return an int and maybe I am just not calling the function correctly. If that is the case, what is considered the correct way to call this function so that it also prints out the information that I need it to? Or is this error completely different than how I am understanding it to be?

A.Torres
  • 413
  • 1
  • 6
  • 16
  • 4
    With `foo(5,1)` you have n=5, k=1, so `n<=k` is false, and `foo(n/k,k)` means `foo(5/1,1)` aka `foo(5,1)`, so calls itself with same parameters, then calls itself with same parameters, then calls itself with same parameters, then calls itself with same parameters, then calls itself with same parameters, then calls itself with same parameters, then calls itself with same parameters, then calls itself with same parameters, then **Stack Overflow** – Andreas Sep 15 '18 at 20:22
  • @Andreas thanks for catching that. I was reading the code in a completely different way. – A.Torres Sep 15 '18 at 20:24

5 Answers5

3

You just have an infinite recursive loop:

foo(5, 1): n = 5, k = 1
calls foo(5 / 1, 1), i.e. foo(5, 1)
calls foo(5 / 1, 1), i.e. foo(5, 1)
calls foo(5 / 1, 1), i.e. foo(5, 1)
...
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

When you call this foo(5, 1); the the argument are 5 and 1 this means logic inside foo if(5<=1) fails and thus code in side else will execute i.e. return foo(5/1,1) + 1; this will call foo again and again, and repeats, and repeats ... resulting in StackOverflowError

foo(5, 1); always calling with out ending foo, so you are getting StackOverflowError try to make this call end

The Scientific Method
  • 2,374
  • 2
  • 14
  • 25
1

You would have gotten a compile error if the problem was a wrong return type. A stackoverflow error is caused when the method uses up all the memory that has been allocated. For more info on how a stackoverflow error is caused read this: What is a StackOverflowError?

Maurice
  • 6,698
  • 9
  • 47
  • 104
1

Seems like you have an infinite recurrent function call, which causes JVM's heap overfill. Try to include some input validation to prevent situations like this.

dominikbrandon
  • 408
  • 4
  • 16
1

Your code is running in infinite loop but you can catch it like below

public static int foo(int n, int k){
        long startTime = System.nanoTime();
        if(n<=k){`enter code here`
            long endTime = System.nanoTime();
            System.out.println("checkFoo");
            System.out.println("start time: " +startTime);
            System.out.println("end time: " +endTime);
            return 1;
        }
        else{
            try {
                return foo(n/k,k) + 1;
            } catch (StackOverflowError e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return 1;
            }
        }
    }