title: Stack overflow when calculating the 10,001st prime number in Java
body: <p>I am doing problem 7 of Project Euler. What I am supposed to do is calculate the 10,001<sup>st</sup> prime number. (A prime number is an integer greater than one that is only divisible by itself and one.)</p>
  <p>Here is my current program:</p>
  <pre><code>public class Problem7 {
      public static void main(String args[]) {
          long numberOfPrimes = 0;
          long number = 2;
      
          while (numberOfPrimes &lt; 10001) {
              if (isPrime(number)) {
                  numberOfPrimes++;
              }
              number++;
          }
          System.out.println(&quot;10001st prime: &quot; + number);
      }
  
      public static boolean isPrime(long N) {
          if (N &lt;= 1)
              return false;
          else
              return prime(N, N - 1);
      }
  
      public static boolean prime(long X, long Y) {
          if (Y == 1)
              return true;
          else if (X % Y == 0)
              return false;
          else
              return prime(X, Y - 1);
      }
  }
  </code></pre>
  <p>It works okay with finding, say the 100<sup>th</sup> prime number, but running with very large inputs (such as 10,001) results in stack overflow. How do I fix it?</p>