Skip to content

Instantly share code, notes, and snippets.

@ankurpshah
Created July 27, 2013 09:12
Show Gist options
  • Save ankurpshah/6094337 to your computer and use it in GitHub Desktop.
Save ankurpshah/6094337 to your computer and use it in GitHub Desktop.
Declarative and imperative Factorial program
package math;
public class Factorial {
public static long declarativeFactorial(int n) {
assert n > 0 : "Argument must be greater than 0";
if (n == 1) return 1;
else return n * declarativeFactorial(n-1);
}
public static long imperativeFactorial(int n) {
assert n > 0 : "Argument must be greater than 0";
long result = 1;
for (int i = 2; i<= n; i++) {
result *= i;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment