From 254276b2af0a46124dfc272a5e3904abbff9a9d1 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Tue, 25 Jul 2017 11:09:23 -0700 Subject: [PATCH 1/2] In Number interface: remove one and zero (they can't be static), add compareTo and signum. Modify NaiveNumber accordingly. --- src/org/nwapw/abacus/number/NaiveNumber.java | 13 +++++++++---- src/org/nwapw/abacus/number/Number.java | 5 ++--- 2 files changed, 11 insertions(+), 7 deletions(-) mode change 100644 => 100755 src/org/nwapw/abacus/number/NaiveNumber.java diff --git a/src/org/nwapw/abacus/number/NaiveNumber.java b/src/org/nwapw/abacus/number/NaiveNumber.java old mode 100644 new mode 100755 index 484f773..51fda91 --- a/src/org/nwapw/abacus/number/NaiveNumber.java +++ b/src/org/nwapw/abacus/number/NaiveNumber.java @@ -8,6 +8,9 @@ public class NaiveNumber implements Number { this.value = value; } + static final NaiveNumber ZERO = new NaiveNumber(0); + static final NaiveNumber ONE = new NaiveNumber(1); + @Override public int precision() { return 4; @@ -39,13 +42,14 @@ public class NaiveNumber implements Number { } @Override - public Number zero() { - return new NaiveNumber(0); + public int compareTo(Number number) { + NaiveNumber num = (NaiveNumber) number; + return Double.compare(value, num.value); } @Override - public Number one() { - return new NaiveNumber(1); + public int signum() { + return this.compareTo(ZERO); } @Override @@ -53,5 +57,6 @@ public class NaiveNumber implements Number { if(toClass == this.getClass()) return this; return null; } + } diff --git a/src/org/nwapw/abacus/number/Number.java b/src/org/nwapw/abacus/number/Number.java index 4541287..ce42ff6 100755 --- a/src/org/nwapw/abacus/number/Number.java +++ b/src/org/nwapw/abacus/number/Number.java @@ -8,9 +8,8 @@ public interface Number { Number add(Number summand); Number subtract(Number subtrahend); Number negate(); - - Number zero(); - Number one(); + int compareTo(Number number); + int signum(); Number promoteTo(Class toClass); From 67f8c648dbc050cb1cdbeeede3cd1dbde5f90389 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Tue, 25 Jul 2017 11:12:25 -0700 Subject: [PATCH 2/2] Add factorial (external). --- .../nwapw/abacus/number/FunctionDatabase.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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(){