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?