title: Tail Recursion java
body: <p>I am working on finding the largest prime factor of any given number. The code below works fine for most numbers until I input the number I want which is 600851475143. Once I do this I get stack overflow. I have been reading up on tail recursion and from what I know I should have implemented it right. Yet, I still receive the stack overflow error despite it. Where am I going wrong when it comes to implementing tail recursion?</p>
  
  <pre><code>import java.util.Scanner;
  public class LargestPrimeFactor
  {
      static long number;
      static long answer;
  
      public static void main(String[] args)
      {
          System.out.println( "Enter number to find its largest prime factor");
          Scanner input = new Scanner(System.in);
          number = input.nextLong();
  
          answer = largestPrime(number);
          System.out.println("The largest prime of " + number + " is " + answer);
      }
  
      private static long largestPrime(long n)
      {
          n = n-1;    
          if(n % 2 != 0 &amp; number % n == 0 )
          {   
              return n;
          }
          else
          {           
              return largestPrime(n);
          }
      }
  }
  </code></pre>