Created
July 27, 2013 09:12
-
-
Save ankurpshah/6094337 to your computer and use it in GitHub Desktop.
Declarative and imperative Factorial program
This file contains 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
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