Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
Created February 10, 2015 23:51
Show Gist options
  • Save dmnugent80/4be9debea3f954b05a79 to your computer and use it in GitHub Desktop.
Save dmnugent80/4be9debea3f954b05a79 to your computer and use it in GitHub Desktop.
Factorial Tail Recursion Accumulator
public class FactorialSum
{
public static void main(String[] args)
{
Factorial myObject = new Factorial();
long fact = myObject.factorial(10);
System.out.print("factorial: " + fact);
}
}
public class Factorial
{
public long factorial(long n){
return factorialHelper(n, n);
}
public long factorialHelper(long n, long acc){
if (n == 1) return acc;
acc = acc * (n-1);
return (factorialHelper(n - 1, acc));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment