spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[LCM] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-06-10 03:57:44
stats
8 file(s) changed, 88 insertions(+), 6 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Math.js
src/Formulas/AllFormulas.ts
src/Formulas/Math.ts
tests/Formulas/MathTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index bb9fb93..83b793a 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -532,6 +532,15 @@
  6 @constructor
  7 ```
  8 
  9+### LCM 
 10+
 11+```
 12+  Returns the least common multiple of one or more integers. 
 13+@param values - The values or range whose factors to consider in a calculation to find the least common multiple. 
 14+@returns {number} 
 15+@constructor
 16+```
 17+
 18 ### ABS 
 19 
 20 ```
 21diff --git a/TODO.md b/TODO.md
 22index 2d890cb..2c0eac1 100644
 23--- a/TODO.md
 24+++ b/TODO.md
 25@@ -57,7 +57,6 @@ For example 64 tbs to a qt.
 26 * COUNTBLANK
 27 * FACTDOUBLE
 28 * GAMMALN
 29-* LCM
 30 * MROUND
 31 * MULTINOMIAL
 32 * PRODUCT
 33diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 34index 7e89104..8fb6d1d 100644
 35--- a/dist/Formulas/AllFormulas.js
 36+++ b/dist/Formulas/AllFormulas.js
 37@@ -65,6 +65,7 @@ exports.LT = Math_1.LT;
 38 exports.LTE = Math_1.LTE;
 39 exports.NE = Math_1.NE;
 40 exports.GCD = Math_1.GCD;
 41+exports.LCM = Math_1.LCM;
 42 var Info_1 = require("./Info");
 43 exports.NA = Info_1.NA;
 44 var Lookup_1 = require("./Lookup");
 45diff --git a/dist/Formulas/Math.js b/dist/Formulas/Math.js
 46index 88dee02..e7721c5 100644
 47--- a/dist/Formulas/Math.js
 48+++ b/dist/Formulas/Math.js
 49@@ -28,6 +28,35 @@ var GCD = function () {
 50     return result;
 51 };
 52 exports.GCD = GCD;
 53+/**
 54+ * Returns the least common multiple of one or more integers.
 55+ * @param values - The values or range whose factors to consider in a calculation to find the least common multiple.
 56+ * @returns {number}
 57+ * @constructor
 58+ */
 59+var LCM = function () {
 60+    var values = [];
 61+    for (var _i = 0; _i < arguments.length; _i++) {
 62+        values[_i] = arguments[_i];
 63+    }
 64+    ArgsChecker_1.ArgsChecker.checkAtLeastLength(arguments, 1, "LCM");
 65+    // Credits: Jonas Raoni Soares Silva
 66+    var o = Filter_1.Filter.flatten(values);
 67+    for (var i, j, n, d, r = 1; (n = o.pop()) !== undefined;) {
 68+        while (n > 1) {
 69+            if (n % 2) {
 70+                for (i = 3, j = Math.floor(Math.sqrt(n)); i <= j && n % i; i += 2) { }
 71+                d = (i <= j) ? i : n;
 72+            }
 73+            else {
 74+                d = 2;
 75+            }
 76+            for (n /= d, r *= d, i = o.length; i; (o[--i] % d) === 0 && (o[i] /= d) === 1 && o.splice(i, 1)) { }
 77+        }
 78+    }
 79+    return r;
 80+};
 81+exports.LCM = LCM;
 82 /**
 83  * Returns the absolute value of a number.
 84  * @param value to get the absolute value of.
 85diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 86index a3132c7..962e5fc 100644
 87--- a/src/Formulas/AllFormulas.ts
 88+++ b/src/Formulas/AllFormulas.ts
 89@@ -62,7 +62,8 @@ import {
 90   LT,
 91   LTE,
 92   NE,
 93-  GCD
 94+  GCD,
 95+  LCM
 96 } from "./Math";
 97 import {
 98   NA
 99@@ -303,5 +304,6 @@ export {
100   NA,
101   CHOOSE,
102   GCD,
103-  TRIM
104+  TRIM,
105+  LCM
106 }
107\ No newline at end of file
108diff --git a/src/Formulas/Math.ts b/src/Formulas/Math.ts
109index ae5e7ba..cdab882 100644
110--- a/src/Formulas/Math.ts
111+++ b/src/Formulas/Math.ts
112@@ -43,6 +43,31 @@ var GCD = function (...values) {
113 };
114 
115 
116+/**
117+ * Returns the least common multiple of one or more integers.
118+ * @param values - The values or range whose factors to consider in a calculation to find the least common multiple.
119+ * @returns {number}
120+ * @constructor
121+ */
122+var LCM =  function (...values) {
123+  ArgsChecker.checkAtLeastLength(arguments, 1, "LCM");
124+  // Credits: Jonas Raoni Soares Silva
125+  var o = Filter.flatten(values);
126+  for (var i, j, n, d, r = 1; (n = o.pop()) !== undefined;) {
127+    while (n > 1) {
128+      if (n % 2) {
129+        for (i = 3, j = Math.floor(Math.sqrt(n)); i <= j && n % i; i += 2) {}
130+        d = (i <= j) ? i : n;
131+      } else {
132+        d = 2;
133+      }
134+      for (n /= d, r *= d, i = o.length; i; (o[--i] % d) === 0 && (o[i] /= d) === 1 && o.splice(i, 1)) {}
135+    }
136+  }
137+  return r;
138+};
139+
140+
141 /**
142  * Returns the absolute value of a number.
143  * @param value to get the absolute value of.
144@@ -1260,5 +1285,6 @@ export {
145   LT,
146   LTE,
147   NE,
148-  GCD
149+  GCD,
150+  LCM
151 }
152\ No newline at end of file
153diff --git a/tests/Formulas/MathTest.ts b/tests/Formulas/MathTest.ts
154index 3b150d6..48994b0 100644
155--- a/tests/Formulas/MathTest.ts
156+++ b/tests/Formulas/MathTest.ts
157@@ -62,7 +62,8 @@ import {
158   LT,
159   LTE,
160   NE,
161-  GCD
162+  GCD,
163+  LCM
164 } from "../../src/Formulas/Math";
165 import * as ERRORS from "../../src/Errors";
166 import {
167@@ -72,6 +73,16 @@ import {
168 } from "../Utils/Asserts";
169 
170 
171+test("LCM", function(){
172+  assertEquals(LCM(2, 5), 10);
173+  assertEquals(LCM(10, 100), 100);
174+  assertEquals(LCM(12, 18), 36);
175+  assertEquals(LCM(12, 18, 24), 72);
176+  catchAndAssertEquals(function() {
177+    LCM.apply(this, []);
178+  }, ERRORS.NA_ERROR);
179+});
180+
181 test("GCD", function(){
182   assertEquals(GCD(10, 100), 10);
183   assertEquals(GCD(22, 44), 22);
184diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
185index 6e61b5a..c8370fb 100644
186--- a/tests/SheetFormulaTest.ts
187+++ b/tests/SheetFormulaTest.ts
188@@ -251,6 +251,10 @@ test("Sheet DEGREES", function(){
189   assertFormulaEquals('=DEGREES(PI())', 180);
190 });
191 
192+test("Sheet LCM", function(){
193+  assertFormulaEquals('=LCM(2, 5)', 10);
194+});
195+
196 test("Sheet DELTA", function(){
197   assertFormulaEquals('=DELTA(2, 2)', 1);
198 });