title: Count div from codility. StackOverflowError in program using recursion
body: <p>I wrote program for one of lessons from Codility. Its called count div. </p>
  
  <p>For example. I give number 6, 11 and 2. There are 3 numbers from 6 to 11 that we can divide by 2, its 6, 8, 10 so method should return 3.</p>
  
  <p>At first I made program with recursion with only ints but I got error so I changed it to BigIntegers, but it doesnt help at all. It's working good for small numbers but with for example input:</p>
  
  <p>A = 0, B = 20000, K = 1 it gives errors:</p>
  
  <pre><code>Exception in thread "main" java.lang.StackOverflowError
  at java.math.MutableBigInteger.divideKnuth(Unknown Source)
  at java.math.MutableBigInteger.divideKnuth(Unknown Source)
  at java.math.BigInteger.remainderKnuth(Unknown Source)
  at java.math.BigInteger.remainder(Unknown Source)
  at java.math.BigInteger.mod(Unknown Source)
  at count_div.Solution.bigIntegerSolution(Solution.java:29)
  at count_div.Solution.bigIntegerSolution(Solution.java:35)
  </code></pre>
  
  <p>Here's my code:</p>
  
  <pre><code>public int solution(int A, int B, int K){
  
      BigInteger minValue = BigInteger.valueOf(A);
      BigInteger maxValue = BigInteger.valueOf(B);
      BigInteger div = BigInteger.valueOf(K);
  
      finalCounter = bigIntegerSolution(minValue, maxValue, div).intValue();
  
      return finalCounter;
  }
  
  public BigInteger bigIntegerSolution(BigInteger minValue, BigInteger maxValue, BigInteger div){
  
      int comparator = minValue.compareTo(maxValue);
  
      if(comparator &lt;= 0){
  
          BigInteger modValue = minValue.mod(div);
  
          if( modValue.compareTo(zero) == 0){
              divCounter = divCounter.add(one);
          }
          minValue = minValue.add(one);
          bigIntegerSolution(minValue, maxValue, div);
      }
  
      return divCounter;
  }
  </code></pre>
  
  <p>Is there anything I can do or my solution idea is just bad for this purpose? I know that they are other solutions but I first came up with this and I would like to know if I can fix it.</p>