Repository: josdejong/mathjs
Pull request #691 (2016-07-22 00:36:54)

Added isPrime method, to check to see if numbers are prime
* Allows to check if numbers (including bignumbers) are prime 
* Included tests

Let me know if this is useful! I'm new to this whole open-source thing, so let me know what I can work or improve. Thanks :)


Patch:
diff --git a/lib/function/utils/index.js b/lib/function/utils/index.js
index 393bdf93a..11a054bec 100644
--- a/lib/function/utils/index.js
+++ b/lib/function/utils/index.js
@@ -4,6 +4,7 @@ module.exports = [
   require('./isNegative'),
   require('./isNumeric'),
   require('./isPositive'),
+  require('./isPrime'),
   require('./isZero'),
   require('./isNaN'),
   require('./typeof')
diff --git a/lib/function/utils/isPrime.js b/lib/function/utils/isPrime.js
new file mode 100644
index 000000000..4b06b406a
--- /dev/null
+++ b/lib/function/utils/isPrime.js
@@ -0,0 +1,81 @@
+'use strict';
+
+var deepMap = require('../../utils/collection/deepMap');
+var number = require('../../utils/number');
+
+function factory (type, config, load, typed) {
+  /**
+   * Test whether a value is prime: has no divisors other than itself and one.
+   * The function supports type `number`, `bignumber`.
+   *
+   * The function is evaluated element-wise in case of Array or Matrix input.
+   *
+   * Syntax:
+   *
+   *     math.isPrime(x)
+   *
+   * Examples:
+   *
+   *    math.isPrime(3);                     // returns true
+   *    math.isPrime(-2);                    // returns false
+   *    math.isPrime(0);                     // returns false
+   *    math.isPrime(-0);                    // returns false
+   *    math.isPrime(0.5);                   // returns false
+   *    math.isPrime('2');                   // returns true
+   *    math.isPrime([2, 17, 100]');           // returns [true, true, false]
+   *
+   * See also:
+   *
+   *    isNumeric, isZero, isNegative, isInteger
+   *
+   * @param {number | BigNumber | Fraction | Unit | Array | Matrix} x  Value to be tested
+   * @return {boolean}  Returns true when `x` is larger than zero.
+   *                    Throws an error in case of an unknown data type.
+   */
+  var isPrime = typed('isPrime', {
+    'number': function (x) {
+      if (x < 2){
+        return false
+      }
+      if (x == 2){
+        return true;
+      }
+      if (x % 2 == 0){ 
+        return false;
+      }
+      for (var i = 3; i * i <= x; i += 2){
+        if (x % i == 0){
+          return false;
+        }
+      }
+      return true;
+    },
+
+    'BigNumber': function (x) {
+      if (x < 2){
+        return false
+      }
+      if (x == 2){
+        return true;
+      }
+      if (x % 2 == 0){ 
+        return false;
+      }
+      for (var i = 3; i * i <= x; i += 2){
+        if (x % i == 0){
+          return false;
+        }
+      }
+      return true;
+    },
+
+    'Array | Matrix': function (x) {
+      return deepMap(x, isPrime);
+    }
+  });
+
+  return isPrime;
+}
+
+exports.name = 'isPrime';
+exports.factory = factory;


Test patch:
diff --git a/test/function/utils/isPrime.test.js b/test/function/utils/isPrime.test.js
new file mode 100644
index 000000000..054b2bea0
--- /dev/null
+++ b/test/function/utils/isPrime.test.js
@@ -0,0 +1,49 @@
+var assert = require('assert');
+var math = require('../../../index');
+var isPrime = math.isPrime;
+var bignumber = math.bignumber;
+var complex = math.complex;
+
+describe('isPrime', function() {
+
+  it('should test whether a number is prime', function() {
+    assert.strictEqual(isPrime(0), false);
+    assert.strictEqual(isPrime(-0), false);
+    assert.strictEqual(isPrime(-1), false);
+    assert.strictEqual(isPrime(1), false);
+    assert.strictEqual(isPrime(2), true);
+    assert.strictEqual(isPrime(3), true);
+    assert.strictEqual(isPrime(5), true);
+    assert.strictEqual(isPrime(7), true);
+    assert.strictEqual(isPrime(4), false);
+    assert.strictEqual(isPrime(100), false);
+    assert.strictEqual(isPrime(102), false);
+    assert.strictEqual(isPrime(999), false);
+  });
+
+  it('should test whether a BigNumber is prime', function() {
+    assert.strictEqual(isPrime(bignumber(0)), false);
+    assert.strictEqual(isPrime(bignumber(-0)), false);
+    assert.strictEqual(isPrime(bignumber(-1)), false);
+    assert.strictEqual(isPrime(bignumber(1)), false);
+    assert.strictEqual(isPrime(bignumber(2)), true);
+    assert.strictEqual(isPrime(bignumber(3)), true);
+    assert.strictEqual(isPrime(bignumber(5)), true);
+    assert.strictEqual(isPrime(bignumber(7)), true);
+    assert.strictEqual(isPrime(bignumber(4)), false);
+    assert.strictEqual(isPrime(bignumber(100)), false);
+    assert.strictEqual(isPrime(bignumber(102)), false);
+    assert.strictEqual(isPrime(bignumber(999)), false);
+  });
+
+  it('should test isPrime element wise on an Array', function() {
+    assert.deepEqual(isPrime([0, 1, 2, 5, 9]), [false, false, true, true, false]);
+  });
+
+  it('should throw an error in case of unsupported data types', function() {
+    assert.throws(function () {isPrime(complex(2, 3))}, /TypeError: Unexpected type of argument/);
+    assert.throws(function () {isPrime(new Date())}, /TypeError: Unexpected type of argument/);
+    assert.throws(function () {isPrime({})}, /TypeError: Unexpected type of argument/);
+  });
+
+});