i am practicing my java by counting possible ways to reach n with a dice. and when input n value to smaller number, it works. but when i input n value to 100, it got infinite looping.
can u guys help me ?
here is my code :
public static void main(String[] args) { testCode test = new testCode(); System.out.println(test.countWays(100)); } private static int countWays(int n) { if(n<=1) { return 1; } else { System.out.println("counting ...."); return countWays(n-6) + countWays(n-5) + countWays(n-4) + countWays(n-3) + countWays(n-2) + countWays(n-1); } }
countWayswithn > 1, you trigger six further calls to it. Each of those triggers six further calls (e.g., 36 more; 43 so far if you're counting); each of those 36 triggers six further calls (259 so far), and so on, and so on.