diff --git a/src/org/nwapw/abacus/number/FunctionDatabase.java b/src/org/nwapw/abacus/number/FunctionDatabase.java index 891c009..909c9b7 100755 --- a/src/org/nwapw/abacus/number/FunctionDatabase.java +++ b/src/org/nwapw/abacus/number/FunctionDatabase.java @@ -7,7 +7,6 @@ public class FunctionDatabase { private HashMap functions; private void registerDefault(){ - functions.put("+", new Function() { @Override protected boolean matchesParams(Number[] params) { @@ -63,6 +62,25 @@ public class FunctionDatabase { return params[0].divide(params[1]); } }); + + functions.put("!", new Function() { + @Override + protected boolean matchesParams(Number[] params) { + return params.length == 1; + } + + @Override + protected Number applyInternal(Number[] params) { + Number factorial = params[0]; + Number multiplier = params[0]; + //It is necessary to later prevent calls of factorial on anything but non-negative integers. + while(multiplier.signum() == 1){ + multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass())); + factorial = factorial.multiply(multiplier); + } + return factorial; + } + }); } public FunctionDatabase(){