Created
February 10, 2015 23:51
-
-
Save dmnugent80/4be9debea3f954b05a79 to your computer and use it in GitHub Desktop.
Factorial Tail Recursion Accumulator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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