Skip to content

Instantly share code, notes, and snippets.

@icarocamelo
Created September 24, 2011 00:50
Show Gist options
  • Save icarocamelo/1238793 to your computer and use it in GitHub Desktop.
Save icarocamelo/1238793 to your computer and use it in GitHub Desktop.
Factorial: OO x OO (FP inspired)
public class Factorial {
public static long declarativeFactorial(int n){
assert n > 0 : "Argument must be greaten 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 greaten 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