1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-22 15:30:09 -08:00

Add factorial (external).

This commit is contained in:
Arthur Drobot 2017-07-25 11:12:25 -07:00
parent 254276b2af
commit 67f8c648db

View File

@ -7,7 +7,6 @@ public class FunctionDatabase {
private HashMap<String, Function> functions; private HashMap<String, Function> functions;
private void registerDefault(){ private void registerDefault(){
functions.put("+", new Function() { functions.put("+", new Function() {
@Override @Override
protected boolean matchesParams(Number[] params) { protected boolean matchesParams(Number[] params) {
@ -63,6 +62,25 @@ public class FunctionDatabase {
return params[0].divide(params[1]); 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(){ public FunctionDatabase(){