Last active
March 21, 2020 16:25
-
-
Save mekilis/21570b70038903dfb31cd241bd5846ca to your computer and use it in GitHub Desktop.
The factorial function using recursion
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
private static long factorialTopDownRecursion(long n) { | |
// base case | |
if (n <= 1) | |
return 1; | |
return n * factorialTopDownRecursion(n-1); | |
} | |
private static long factorialBottomUpRecursion(long n) { | |
return _factorialBottomUpRecursion(1, n); | |
} | |
private static long _factorialBottomUpRecursion(long n, long upperBound) { | |
// base case | |
if (n > upperBound) | |
return 1; | |
return n * _factorialBottomUpRecursion(n+1, upperBound); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment