mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 07:20:09 -08:00
Add factorial (external).
This commit is contained in:
parent
254276b2af
commit
67f8c648db
|
@ -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(){
|
||||||
|
|
Loading…
Reference in New Issue
Block a user