From fc4e8493e30def5f5afed531e936323c7466ce31 Mon Sep 17 00:00:00 2001 From: Kenan Yildirim Date: Sat, 18 May 2013 20:12:17 -0400 Subject: [PATCH] Migrate and modulate utility functions --- .gitignore | 2 +- .jshintrc | 21 - Gruntfile.js | 16 +- component.json | 27 ++ dist/primality.js | 809 +++++++++++++++++++++++++++++++++++++ doc/README.md | 37 +- lib/util/common.js | 46 +++ lib/util/createCallback.js | 52 +++ lib/util/forIn.js | 17 + lib/util/identity.js | 5 + lib/util/index.js | 8 + lib/util/indexOf.js | 23 ++ lib/util/isArray.js | 3 + lib/util/isEqual.js | 141 +++++++ lib/util/isFinite.js | 8 + lib/util/isFunction.js | 5 + lib/util/isNaN.js | 7 + lib/util/isNumber.js | 8 + lib/util/isObject.js | 7 + lib/util/keys.js | 25 ++ lib/util/sortedIndex.js | 20 + package.json | 1 + primality.js | 405 ++++++++----------- primality.min.js | 2 +- test/spec/suite.js | 8 +- 25 files changed, 1430 insertions(+), 273 deletions(-) delete mode 100644 .jshintrc create mode 100644 component.json create mode 100644 dist/primality.js create mode 100644 lib/util/common.js create mode 100644 lib/util/createCallback.js create mode 100644 lib/util/forIn.js create mode 100644 lib/util/identity.js create mode 100644 lib/util/index.js create mode 100644 lib/util/indexOf.js create mode 100644 lib/util/isArray.js create mode 100644 lib/util/isEqual.js create mode 100644 lib/util/isFinite.js create mode 100644 lib/util/isFunction.js create mode 100644 lib/util/isNaN.js create mode 100644 lib/util/isNumber.js create mode 100644 lib/util/isObject.js create mode 100644 lib/util/keys.js create mode 100644 lib/util/sortedIndex.js diff --git a/.gitignore b/.gitignore index 9303c34..5171c54 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -node_modules/ +node_modules npm-debug.log \ No newline at end of file diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 53eb3e2..0000000 --- a/.jshintrc +++ /dev/null @@ -1,21 +0,0 @@ -{ - "camelcase": true, - "eqnull": true, - "es5" : true, - "noarg": true, - "quotmark": true, - "trailing": true, - "undef": true, - "unused": true, - "white": false, - - "indent": 2, - - "browser": true, - "node": true, - "rhino": true, - - "predef": [ - "define" - ] -} \ No newline at end of file diff --git a/Gruntfile.js b/Gruntfile.js index c364dfc..7b3b6c4 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,9 +1,15 @@ module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), - jshint: { - all: ['grunt.js', 'primality.js'], - options: grunt.file.readJSON('.jshintrc') + shell: { + build: { + options: { + stdout: true, + stderr: true, + failOnError: true + }, + command: 'component build -o dist -n primality -s primality' + } }, uglify: { options: { @@ -48,10 +54,10 @@ module.exports = function(grunt) { }); }); - grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-mocha'); + grunt.loadNpmTasks('grunt-shell'); grunt.registerTask('test', ['mocha']); - grunt.registerTask('default', ['test', 'uglify', 'doc']); + grunt.registerTask('default', ['shell:build', 'test', 'uglify', 'doc']); }; \ No newline at end of file diff --git a/component.json b/component.json new file mode 100644 index 0000000..7af3f17 --- /dev/null +++ b/component.json @@ -0,0 +1,27 @@ +{ + "name": "primality", + "repo": "KenanY/primality", + "description": "JavaScript library for prime numbers.", + "version": "1.4.0", + "keywords": ["math"], + "main": "primality.js", + "scripts": [ + "primality.js", + "lib/util/index.js", + "lib/util/common.js", + "lib/util/createCallback.js", + "lib/util/forIn.js", + "lib/util/identity.js", + "lib/util/indexOf.js", + "lib/util/isArray.js", + "lib/util/isEqual.js", + "lib/util/isFinite.js", + "lib/util/isFunction.js", + "lib/util/isNaN.js", + "lib/util/isNumber.js", + "lib/util/isObject.js", + "lib/util/keys.js", + "lib/util/sortedIndex.js" + ], + "license": "MIT" +} \ No newline at end of file diff --git a/dist/primality.js b/dist/primality.js new file mode 100644 index 0000000..b5ce3df --- /dev/null +++ b/dist/primality.js @@ -0,0 +1,809 @@ +;(function(){ + +/** + * Require the given path. + * + * @param {String} path + * @return {Object} exports + * @api public + */ + +function require(path, parent, orig) { + var resolved = require.resolve(path); + + // lookup failed + if (null == resolved) { + orig = orig || path; + parent = parent || 'root'; + var err = new Error('Failed to require "' + orig + '" from "' + parent + '"'); + err.path = orig; + err.parent = parent; + err.require = true; + throw err; + } + + var module = require.modules[resolved]; + + // perform real require() + // by invoking the module's + // registered function + if (!module.exports) { + module.exports = {}; + module.client = module.component = true; + module.call(this, module.exports, require.relative(resolved), module); + } + + return module.exports; +} + +/** + * Registered modules. + */ + +require.modules = {}; + +/** + * Registered aliases. + */ + +require.aliases = {}; + +/** + * Resolve `path`. + * + * Lookup: + * + * - PATH/index.js + * - PATH.js + * - PATH + * + * @param {String} path + * @return {String} path or null + * @api private + */ + +require.resolve = function(path) { + if (path.charAt(0) === '/') path = path.slice(1); + var index = path + '/index.js'; + + var paths = [ + path, + path + '.js', + path + '.json', + path + '/index.js', + path + '/index.json' + ]; + + for (var i = 0; i < paths.length; i++) { + var path = paths[i]; + if (require.modules.hasOwnProperty(path)) return path; + } + + if (require.aliases.hasOwnProperty(index)) { + return require.aliases[index]; + } +}; + +/** + * Normalize `path` relative to the current path. + * + * @param {String} curr + * @param {String} path + * @return {String} + * @api private + */ + +require.normalize = function(curr, path) { + var segs = []; + + if ('.' != path.charAt(0)) return path; + + curr = curr.split('/'); + path = path.split('/'); + + for (var i = 0; i < path.length; ++i) { + if ('..' == path[i]) { + curr.pop(); + } else if ('.' != path[i] && '' != path[i]) { + segs.push(path[i]); + } + } + + return curr.concat(segs).join('/'); +}; + +/** + * Register module at `path` with callback `definition`. + * + * @param {String} path + * @param {Function} definition + * @api private + */ + +require.register = function(path, definition) { + require.modules[path] = definition; +}; + +/** + * Alias a module definition. + * + * @param {String} from + * @param {String} to + * @api private + */ + +require.alias = function(from, to) { + if (!require.modules.hasOwnProperty(from)) { + throw new Error('Failed to alias "' + from + '", it does not exist'); + } + require.aliases[to] = from; +}; + +/** + * Return a require function relative to the `parent` path. + * + * @param {String} parent + * @return {Function} + * @api private + */ + +require.relative = function(parent) { + var p = require.normalize(parent, '..'); + + /** + * lastIndexOf helper. + */ + + function lastIndexOf(arr, obj) { + var i = arr.length; + while (i--) { + if (arr[i] === obj) return i; + } + return -1; + } + + /** + * The relative require() itself. + */ + + function localRequire(path) { + var resolved = localRequire.resolve(path); + return require(resolved, parent, path); + } + + /** + * Resolve relative to the parent. + */ + + localRequire.resolve = function(path) { + var c = path.charAt(0); + if ('/' == c) return path.slice(1); + if ('.' == c) return require.normalize(p, path); + + // resolve deps by returning + // the dep in the nearest "deps" + // directory + var segs = parent.split('/'); + var i = lastIndexOf(segs, 'deps') + 1; + if (!i) i = 0; + path = segs.slice(0, i + 1).join('/') + '/deps/' + path; + return path; + }; + + /** + * Check if module is defined at `path`. + */ + + localRequire.exists = function(path) { + return require.modules.hasOwnProperty(localRequire.resolve(path)); + }; + + return localRequire; +}; +require.register("primality/primality.js", function(exports, require, module){ +/*! + * primality v1.4.0 + * (c) 2012–2013 Kenan Yildirim + * + * Includes functions from Lo-Dash + * (c) 2012–2013 The Dojo Foundation + * + * Available under MIT license + */ +var primality; + +var WILSON_PRIMES = [5, 13, 563]; + +var _ = require('./lib/util/'); + +/** + * Finds the smallest factor of `n` + * + * @private + * @param {Number} value The value to check + * @returns {Number} + * The smallest prime that divides n + * NaN if n is NaN or Infinity + * 0 if n is 0 + * 1 if n = 1, n = -1, or n is not an integer + */ +function leastFactor(n) { + if (n === 0) return 0; + else if (n % 1 || n * n < 2) return 1; + else if (n % 2 === 0) return 2; + else if (n % 3 === 0) return 3; + else if (n % 5 === 0) return 5; + + var m = Math.sqrt(n); + for (var i = 7; i <= m; i += 30) { + if (n % i === 0) return i; + if (n % (i + 4) === 0) return i + 4; + if (n % (i + 6) === 0) return i + 6; + if (n % (i + 10) === 0) return i + 10; + if (n % (i + 12) === 0) return i + 12; + if (n % (i + 16) === 0) return i + 16; + if (n % (i + 22) === 0) return i + 22; + if (n % (i + 24) === 0) return i + 24; + } + return n; +} + +/** + * Checks if `value` is prime. + * + * @private + * @param {Number} value The value to check + * @returns {Boolean} Returns `true` if `value` is prime + */ +function isPrime(value) { + if (_.isNaN(value) || !_.isFinite(value) || value % 1 || value < 2) { + return false; + } + if (value !== leastFactor(value)) return false; + return true; +} + +/** + * Creates a new primality instance. + * + * @name primality + * @constructor + * @param {Mixed} input A number, string, or array to check the primality of. + * @returns {Boolean} Returns `true` if `input` is prime. + * @example + * + * primality(7); + * // => true + * + * primality('13'); + * // => true + * + * primality([17, 19, 23]); + * // => true + */ +primality = function(input) { + if (input === null || input === '') return null; + else if (_.isArray(input)) { + for (var i = 0, l = input.length; i < l; i++) { + if (!isPrime(input[i])) return false; + } + return true; + } + else return isPrime(input); +}; + +/** + * Checks if `a` and `b` are twin primes + * + * + * + * @static + * @memberOf primality + * @param {Number} a First of the pair + * @param {Number} b Second of the pair + * @returns {Boolean} Returns `true` if `a` and `b` are twin primes + * @example + * + * primality.areTwinPrimes(3, 5) + * // => true + */ +function areTwinPrimes(a, b) { + if (Math.abs(a - b) != 2) return false; + if (!primality([a, b])) return false; + return true; +} + +/** + * Checks if `a` and `b` are cousin primes + * + * + * + * @static + * @memberOf primality + * @param {Number} a First of the pair + * @param {Number} b Second of the pair + * @returns {Boolean} Returns `true` if `a` and `b` are cousin primes + * @example + * + * primality.areCousinPrimes(3, 7) + * // => true + */ +function areCousinPrimes(a, b) { + if (Math.abs(a - b) != 4) return false; + if (!primality([a, b])) return false; + return true; +} + +/** + * Checks if `a` and `b` are sexy primes + * + * + * + * @static + * @memberOf primality + * @param {Number} a First of the pair + * @param {Number} b Second of the pair + * @returns {Boolean} Returns `true` if `a` and `b` are sexy primes + * @example + * + * primality.areSexyPrimes(5, 11) + * // => true + */ +function areSexyPrimes(a, b) { + if (Math.abs(a - b) != 6) return false; + if (!primality([a, b])) return false; + return true; +} + +/** + * Checks if `a` is a Wilson prime. + * + * + * + * @static + * @memberOf primality + * @param {Number} a + * @returns {Boolean} Returns `true` if `a` is a Wilson prime. + * @example + * + * primality.isWilsonPrime(5); + * // => true + */ +function isWilsonPrime(a) { + if (_.indexOf(WILSON_PRIMES, a) > -1) { + return true; + } + return false; +} + +/** + * The semantic version number. + * + * @static + * @memberOf primality + * @type String + */ +primality.VERSION = '1.4.0'; + +primality.areTwinPrimes = areTwinPrimes; +primality.areCousinPrimes = areCousinPrimes; +primality.areSexyPrimes = areSexyPrimes; +primality.isWilsonPrime = isWilsonPrime; + +// Expose Primality +(module.exports = primality).primality = primality; +}); +require.register("primality/lib/util/index.js", function(exports, require, module){ +var _ = {}; + +_.indexOf = require('./indexOf'); +_.isArray = require('./isArray'); +_.isFinite = require('./isFinite'); +_.isNaN = require('./isNaN'); + +module.exports = _; +}); +require.register("primality/lib/util/common.js", function(exports, require, module){ +var window = window || {}; + +var freeGlobal = typeof global == 'object' && global; +if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { + window = freeGlobal; +} + +var indicatorObject = {}; +var objectTypes = { + 'boolean': false, + 'function': true, + 'object': true, + 'number': false, + 'string': false, + 'undefined': false +}; +var objectRef = Object(); +var hasOwnProperty = objectRef.hasOwnProperty; + +var reNative = RegExp('^' + + String(objectRef.valueOf) + .replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + .replace(/valueOf|for [^\]]+/g, '.+?') + '$' +); + +var nativeIsArray = reNative.test(nativeIsArray = Array.isArray) && nativeIsArray; +var nativeIsFinite = window.isFinite; +var nativeIsNaN = window.isNaN; +var nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys; +var nativeMax = Math.max; + +var toString = objectRef.toString; + +module.exports = { + hasOwnProperty: hasOwnProperty, + indicatorObject: indicatorObject, + nativeIsArray: nativeIsArray, + nativeIsFinite: nativeIsFinite, + nativeIsNaN: nativeIsNaN, + nativeKeys: nativeKeys, + nativeMax: nativeMax, + objectRef: objectRef, + objectTypes: objectTypes, + reNative: reNative, + toString: toString +}; +}); +require.register("primality/lib/util/createCallback.js", function(exports, require, module){ +var indicatorObject = require('./common').indicatorObject; +var identity = require('./identity'); +var isEqual = require('./isEqual'); +var keys = require('./keys'); + +function createCallback(func, thisArg, argCount) { + if (func == null) { + return identity; + } + var type = typeof func; + if (type != 'function') { + if (type != 'object') { + return function(object) { + return object[func]; + }; + } + var props = keys(func); + return function(object) { + var length = props.length, + result = false; + while (length--) { + if (!(result = isEqual(object[props[length]], func[props[length]], indicatorObject))) { + break; + } + } + return result; + }; + } + if (typeof thisArg != 'undefined') { + if (argCount === 1) { + return function(value) { + return func.call(thisArg, value); + }; + } + if (argCount === 2) { + return function(a, b) { + return func.call(thisArg, a, b); + }; + } + if (argCount === 4) { + return function(accumulator, value, index, collection) { + return func.call(thisArg, accumulator, value, index, collection); + }; + } + return function(value, index, collection) { + return func.call(thisArg, value, index, collection); + }; + } + return func; +} + +module.exports = createCallback; +}); +require.register("primality/lib/util/forIn.js", function(exports, require, module){ +var objectTypes = require('./common').objectTypes; + +var createCallback = require('./createCallback'); + +var forIn = function (collection, callback, thisArg) { + var index, iterable = collection, result = iterable; + if (!iterable) return result; + if (!objectTypes[typeof iterable]) return result; + callback = callback && typeof thisArg == 'undefined' ? callback : createCallback(callback, thisArg); + + for (index in iterable) { + if (callback(iterable[index], index, collection) === false) return result; + } + return result +}; + +module.exports = forIn; +}); +require.register("primality/lib/util/identity.js", function(exports, require, module){ +function identity(value) { + return value; +} + +module.exports = identity; +}); +require.register("primality/lib/util/indexOf.js", function(exports, require, module){ +var sortedIndex = require('./sortedIndex'); + +var nativeMax = require('./common').nativeMax; + +function indexOf(array, value, fromIndex) { + var index = -1, + length = array ? array.length : 0; + + if (typeof fromIndex == 'number') { + index = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0) - 1; + } else if (fromIndex) { + index = sortedIndex(array, value); + return array[index] === value ? index : -1; + } + while (++index < length) { + if (array[index] === value) { + return index; + } + } + return -1; +} + +module.exports = indexOf; +}); +require.register("primality/lib/util/isArray.js", function(exports, require, module){ +var nativeIsArray = require('./common').nativeIsArray; + +module.exports = nativeIsArray; +}); +require.register("primality/lib/util/isEqual.js", function(exports, require, module){ +var hasOwnProperty = require('./common').hasOwnProperty; +var indicatorObject = require('./common').indicatorObject; +var toString = require('./common').toString; + +var createCallback = require('./createCallback'); +var forIn = require('./forIn'); +var isFunction = require('./isFunction'); + +var argsClass = '[object Arguments]'; +var arrayClass = '[object Array]'; +var boolClass = '[object Boolean]'; +var dateClass = '[object Date]'; +var numberClass = '[object Number]'; +var objectClass = '[object Object]'; +var regexpClass = '[object RegExp]'; +var stringClass = '[object String]'; + +function isEqual(a, b, callback, thisArg, stackA, stackB) { + var whereIndicator = callback === indicatorObject; + if (typeof callback == 'function' && !whereIndicator) { + callback = createCallback(callback, thisArg, 2); + var result = callback(a, b); + if (typeof result != 'undefined') { + return !!result; + } + } + if (a === b) { + return a !== 0 || (1 / a == 1 / b); + } + var type = typeof a, + otherType = typeof b; + + if (a === a && + (!a || (type != 'function' && type != 'object')) && + (!b || (otherType != 'function' && otherType != 'object'))) { + return false; + } + if (a == null || b == null) { + return a === b; + } + var className = toString.call(a), + otherClass = toString.call(b); + + if (className == argsClass) { + className = objectClass; + } + if (otherClass == argsClass) { + otherClass = objectClass; + } + if (className != otherClass) { + return false; + } + switch (className) { + case boolClass: + case dateClass: + return +a == +b; + + case numberClass: + return (a != +a) + ? b != +b + : (a == 0 ? (1 / a == 1 / b) : a == +b); + + case regexpClass: + case stringClass: + return a == String(b); + } + var isArr = className == arrayClass; + if (!isArr) { + if (hasOwnProperty.call(a, '__wrapped__ ') || hasOwnProperty.call(b, '__wrapped__')) { + return isEqual(a.__wrapped__ || a, b.__wrapped__ || b, callback, thisArg, stackA, stackB); + } + if (className != objectClass) { + return false; + } + var ctorA = a.constructor, + ctorB = b.constructor; + + if (ctorA != ctorB && !( + isFunction(ctorA) && ctorA instanceof ctorA && + isFunction(ctorB) && ctorB instanceof ctorB + )) { + return false; + } + } + stackA || (stackA = []); + stackB || (stackB = []); + + var length = stackA.length; + while (length--) { + if (stackA[length] == a) { + return stackB[length] == b; + } + } + var size = 0; + result = true; + + stackA.push(a); + stackB.push(b); + + if (isArr) { + length = a.length; + size = b.length; + + result = size == a.length; + if (!result && !whereIndicator) { + return result; + } + while (size--) { + var index = length, + value = b[size]; + + if (whereIndicator) { + while (index--) { + if ((result = isEqual(a[index], value, callback, thisArg, stackA, stackB))) { + break; + } + } + } else if (!(result = isEqual(a[size], value, callback, thisArg, stackA, stackB))) { + break; + } + } + return result; + } + forIn(b, function(value, key, b) { + if (hasOwnProperty.call(b, key)) { + size++; + return (result = hasOwnProperty.call(a, key) && isEqual(a[key], value, callback, thisArg, stackA, stackB)); + } + }); + + if (result && !whereIndicator) { + forIn(a, function(value, key, a) { + if (hasOwnProperty.call(a, key)) { + return (result = --size > -1); + } + }); + } + return result; +} + +module.exports = isEqual; +}); +require.register("primality/lib/util/isFinite.js", function(exports, require, module){ +var nativeIsFinite = require('./common').nativeIsFinite; +var nativeIsNaN = require('./common').nativeIsNaN; + +function isFinite(value) { + return nativeIsFinite(value) && !nativeIsNaN(parseFloat(value)); +} + +module.exports = isFinite; +}); +require.register("primality/lib/util/isFunction.js", function(exports, require, module){ +function isFunction(value) { + return typeof value == 'function'; +} + +module.exports = isFunction; +}); +require.register("primality/lib/util/isNaN.js", function(exports, require, module){ +var isNumber = require('./isNumber'); + +function isNaN(value) { + return isNumber(value) && value != +value +} + +module.exports = isNaN; +}); +require.register("primality/lib/util/isNumber.js", function(exports, require, module){ +var toString = require('./common').toString; +var numberClass = '[object Number]'; + +function isNumber(value) { + return typeof value == 'number' || toString.call(value) == numberClass; +} + +module.exports = isNumber; +}); +require.register("primality/lib/util/isObject.js", function(exports, require, module){ +var objectTypes = require('./common').objectTypes; + +function isObject(value) { + return value ? objectTypes[typeof value] : false; +} + +module.exports = isObject; +}); +require.register("primality/lib/util/keys.js", function(exports, require, module){ +var hasOwnProperty = require('./common').hasOwnProperty; +var nativeKeys = require('./common').nativeKeys; +var objectTypes = require('./common').objectTypes; + +var isObject = require('./isObject'); + +var shimKeys = function (object) { + var index, iterable = object, result = []; + if (!iterable) return result; + if (!(objectTypes[typeof object])) return result; + + for (index in iterable) { + if (hasOwnProperty.call(iterable, index)) { + result.push(index); + } + } + return result +}; + +module.exports = !nativeKeys ? shimKeys : function(object) { + if (!isObject(object)) { + return []; + } + return nativeKeys(object); +}; +}); +require.register("primality/lib/util/sortedIndex.js", function(exports, require, module){ +var createCallback = require('./createCallback'); +var identity = require('./identity'); + +function sortedIndex(array, value, callback, thisArg) { + var low = 0, + high = array ? array.length : low; + + callback = callback ? createCallback(callback, thisArg, 1) : identity; + value = callback(value); + + while (low < high) { + var mid = (low + high) >>> 1; + (callback(array[mid]) < value) + ? low = mid + 1 + : high = mid; + } + return low; +} + +module.exports = sortedIndex; +}); +require.alias("primality/primality.js", "primality/index.js"); + +if (typeof exports == "object") { + module.exports = require("primality"); +} else if (typeof define == "function" && define.amd) { + define(function(){ return require("primality"); }); +} else { + this["primality"] = require("primality"); +}})(); \ No newline at end of file diff --git a/doc/README.md b/doc/README.md index 0d0ac91..54fc30d 100644 --- a/doc/README.md +++ b/doc/README.md @@ -10,6 +10,7 @@ * [`primality.areCousinPrimes`](#primalityarecousinprimesa-b) * [`primality.areSexyPrimes`](#primalityaresexyprimesa-b) * [`primality.areTwinPrimes`](#primalityaretwinprimesa-b) +* [`primality.isWilsonPrime`](#primalityiswilsonprimea) @@ -35,7 +36,7 @@ ### `primality(input)` -# [Ⓢ](https://github.com/KenanY/primality/blob/master/primality.js#L136 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/KenanY/primality/blob/master/primality.js#L81 "View in source") [Ⓣ][1] Creates a new primality instance. @@ -65,7 +66,7 @@ primality([17, 19, 23]); ### `primality.areCousinPrimes(a, b)` -# [Ⓢ](https://github.com/KenanY/primality/blob/master/primality.js#L183 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/KenanY/primality/blob/master/primality.js#L128 "View in source") [Ⓣ][1] Checks if `a` and `b` are cousin primes @@ -92,7 +93,7 @@ primality.areCousinPrimes(3, 7) ### `primality.areSexyPrimes(a, b)` -# [Ⓢ](https://github.com/KenanY/primality/blob/master/primality.js#L204 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/KenanY/primality/blob/master/primality.js#L149 "View in source") [Ⓣ][1] Checks if `a` and `b` are sexy primes @@ -119,7 +120,7 @@ primality.areSexyPrimes(5, 11) ### `primality.areTwinPrimes(a, b)` -# [Ⓢ](https://github.com/KenanY/primality/blob/master/primality.js#L162 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/KenanY/primality/blob/master/primality.js#L107 "View in source") [Ⓣ][1] Checks if `a` and `b` are twin primes @@ -143,6 +144,32 @@ primality.areTwinPrimes(3, 5) + + +### `primality.isWilsonPrime(a)` +# [Ⓢ](https://github.com/KenanY/primality/blob/master/primality.js#L169 "View in source") [Ⓣ][1] + +Checks if `a` is a Wilson prime. + + + +#### Arguments +1. `a` *(Number)*: + +#### Returns +*(Boolean)*: Returns `true` if `a` is a Wilson prime. + +#### Example +```js +primality.isWilsonPrime(5); +// => true +``` + +* * * + + + + @@ -153,7 +180,7 @@ primality.areTwinPrimes(3, 5) ### `primality.VERSION` -# [Ⓢ](https://github.com/KenanY/primality/blob/master/primality.js#L217 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/KenanY/primality/blob/master/primality.js#L183 "View in source") [Ⓣ][1] *(String)*: The semantic version number. diff --git a/lib/util/common.js b/lib/util/common.js new file mode 100644 index 0000000..0ee523b --- /dev/null +++ b/lib/util/common.js @@ -0,0 +1,46 @@ +var window = window || {}; + +var freeGlobal = typeof global == 'object' && global; +if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { + window = freeGlobal; +} + +var indicatorObject = {}; +var objectTypes = { + 'boolean': false, + 'function': true, + 'object': true, + 'number': false, + 'string': false, + 'undefined': false +}; +var objectRef = Object(); +var hasOwnProperty = objectRef.hasOwnProperty; + +var reNative = RegExp('^' + + String(objectRef.valueOf) + .replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + .replace(/valueOf|for [^\]]+/g, '.+?') + '$' +); + +var nativeIsArray = reNative.test(nativeIsArray = Array.isArray) && nativeIsArray; +var nativeIsFinite = window.isFinite; +var nativeIsNaN = window.isNaN; +var nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys; +var nativeMax = Math.max; + +var toString = objectRef.toString; + +module.exports = { + hasOwnProperty: hasOwnProperty, + indicatorObject: indicatorObject, + nativeIsArray: nativeIsArray, + nativeIsFinite: nativeIsFinite, + nativeIsNaN: nativeIsNaN, + nativeKeys: nativeKeys, + nativeMax: nativeMax, + objectRef: objectRef, + objectTypes: objectTypes, + reNative: reNative, + toString: toString +}; \ No newline at end of file diff --git a/lib/util/createCallback.js b/lib/util/createCallback.js new file mode 100644 index 0000000..00cb5c2 --- /dev/null +++ b/lib/util/createCallback.js @@ -0,0 +1,52 @@ +var indicatorObject = require('./common').indicatorObject; +var identity = require('./identity'); +var isEqual = require('./isEqual'); +var keys = require('./keys'); + +function createCallback(func, thisArg, argCount) { + if (func == null) { + return identity; + } + var type = typeof func; + if (type != 'function') { + if (type != 'object') { + return function(object) { + return object[func]; + }; + } + var props = keys(func); + return function(object) { + var length = props.length, + result = false; + while (length--) { + if (!(result = isEqual(object[props[length]], func[props[length]], indicatorObject))) { + break; + } + } + return result; + }; + } + if (typeof thisArg != 'undefined') { + if (argCount === 1) { + return function(value) { + return func.call(thisArg, value); + }; + } + if (argCount === 2) { + return function(a, b) { + return func.call(thisArg, a, b); + }; + } + if (argCount === 4) { + return function(accumulator, value, index, collection) { + return func.call(thisArg, accumulator, value, index, collection); + }; + } + return function(value, index, collection) { + return func.call(thisArg, value, index, collection); + }; + } + return func; +} + +module.exports = createCallback; \ No newline at end of file diff --git a/lib/util/forIn.js b/lib/util/forIn.js new file mode 100644 index 0000000..3b49cbf --- /dev/null +++ b/lib/util/forIn.js @@ -0,0 +1,17 @@ +var objectTypes = require('./common').objectTypes; + +var createCallback = require('./createCallback'); + +var forIn = function (collection, callback, thisArg) { + var index, iterable = collection, result = iterable; + if (!iterable) return result; + if (!objectTypes[typeof iterable]) return result; + callback = callback && typeof thisArg == 'undefined' ? callback : createCallback(callback, thisArg); + + for (index in iterable) { + if (callback(iterable[index], index, collection) === false) return result; + } + return result +}; + +module.exports = forIn; \ No newline at end of file diff --git a/lib/util/identity.js b/lib/util/identity.js new file mode 100644 index 0000000..d705258 --- /dev/null +++ b/lib/util/identity.js @@ -0,0 +1,5 @@ +function identity(value) { + return value; +} + +module.exports = identity; \ No newline at end of file diff --git a/lib/util/index.js b/lib/util/index.js new file mode 100644 index 0000000..ec78f51 --- /dev/null +++ b/lib/util/index.js @@ -0,0 +1,8 @@ +var _ = {}; + +_.indexOf = require('./indexOf'); +_.isArray = require('./isArray'); +_.isFinite = require('./isFinite'); +_.isNaN = require('./isNaN'); + +module.exports = _; \ No newline at end of file diff --git a/lib/util/indexOf.js b/lib/util/indexOf.js new file mode 100644 index 0000000..759adb2 --- /dev/null +++ b/lib/util/indexOf.js @@ -0,0 +1,23 @@ +var sortedIndex = require('./sortedIndex'); + +var nativeMax = require('./common').nativeMax; + +function indexOf(array, value, fromIndex) { + var index = -1, + length = array ? array.length : 0; + + if (typeof fromIndex == 'number') { + index = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0) - 1; + } else if (fromIndex) { + index = sortedIndex(array, value); + return array[index] === value ? index : -1; + } + while (++index < length) { + if (array[index] === value) { + return index; + } + } + return -1; +} + +module.exports = indexOf; \ No newline at end of file diff --git a/lib/util/isArray.js b/lib/util/isArray.js new file mode 100644 index 0000000..eecd923 --- /dev/null +++ b/lib/util/isArray.js @@ -0,0 +1,3 @@ +var nativeIsArray = require('./common').nativeIsArray; + +module.exports = nativeIsArray; \ No newline at end of file diff --git a/lib/util/isEqual.js b/lib/util/isEqual.js new file mode 100644 index 0000000..20243bd --- /dev/null +++ b/lib/util/isEqual.js @@ -0,0 +1,141 @@ +var hasOwnProperty = require('./common').hasOwnProperty; +var indicatorObject = require('./common').indicatorObject; +var toString = require('./common').toString; + +var createCallback = require('./createCallback'); +var forIn = require('./forIn'); +var isFunction = require('./isFunction'); + +var argsClass = '[object Arguments]'; +var arrayClass = '[object Array]'; +var boolClass = '[object Boolean]'; +var dateClass = '[object Date]'; +var numberClass = '[object Number]'; +var objectClass = '[object Object]'; +var regexpClass = '[object RegExp]'; +var stringClass = '[object String]'; + +function isEqual(a, b, callback, thisArg, stackA, stackB) { + var whereIndicator = callback === indicatorObject; + if (typeof callback == 'function' && !whereIndicator) { + callback = createCallback(callback, thisArg, 2); + var result = callback(a, b); + if (typeof result != 'undefined') { + return !!result; + } + } + if (a === b) { + return a !== 0 || (1 / a == 1 / b); + } + var type = typeof a, + otherType = typeof b; + + if (a === a && + (!a || (type != 'function' && type != 'object')) && + (!b || (otherType != 'function' && otherType != 'object'))) { + return false; + } + if (a == null || b == null) { + return a === b; + } + var className = toString.call(a), + otherClass = toString.call(b); + + if (className == argsClass) { + className = objectClass; + } + if (otherClass == argsClass) { + otherClass = objectClass; + } + if (className != otherClass) { + return false; + } + switch (className) { + case boolClass: + case dateClass: + return +a == +b; + + case numberClass: + return (a != +a) + ? b != +b + : (a == 0 ? (1 / a == 1 / b) : a == +b); + + case regexpClass: + case stringClass: + return a == String(b); + } + var isArr = className == arrayClass; + if (!isArr) { + if (hasOwnProperty.call(a, '__wrapped__ ') || hasOwnProperty.call(b, '__wrapped__')) { + return isEqual(a.__wrapped__ || a, b.__wrapped__ || b, callback, thisArg, stackA, stackB); + } + if (className != objectClass) { + return false; + } + var ctorA = a.constructor, + ctorB = b.constructor; + + if (ctorA != ctorB && !( + isFunction(ctorA) && ctorA instanceof ctorA && + isFunction(ctorB) && ctorB instanceof ctorB + )) { + return false; + } + } + stackA || (stackA = []); + stackB || (stackB = []); + + var length = stackA.length; + while (length--) { + if (stackA[length] == a) { + return stackB[length] == b; + } + } + var size = 0; + result = true; + + stackA.push(a); + stackB.push(b); + + if (isArr) { + length = a.length; + size = b.length; + + result = size == a.length; + if (!result && !whereIndicator) { + return result; + } + while (size--) { + var index = length, + value = b[size]; + + if (whereIndicator) { + while (index--) { + if ((result = isEqual(a[index], value, callback, thisArg, stackA, stackB))) { + break; + } + } + } else if (!(result = isEqual(a[size], value, callback, thisArg, stackA, stackB))) { + break; + } + } + return result; + } + forIn(b, function(value, key, b) { + if (hasOwnProperty.call(b, key)) { + size++; + return (result = hasOwnProperty.call(a, key) && isEqual(a[key], value, callback, thisArg, stackA, stackB)); + } + }); + + if (result && !whereIndicator) { + forIn(a, function(value, key, a) { + if (hasOwnProperty.call(a, key)) { + return (result = --size > -1); + } + }); + } + return result; +} + +module.exports = isEqual; \ No newline at end of file diff --git a/lib/util/isFinite.js b/lib/util/isFinite.js new file mode 100644 index 0000000..5505ae8 --- /dev/null +++ b/lib/util/isFinite.js @@ -0,0 +1,8 @@ +var nativeIsFinite = require('./common').nativeIsFinite; +var nativeIsNaN = require('./common').nativeIsNaN; + +function isFinite(value) { + return nativeIsFinite(value) && !nativeIsNaN(parseFloat(value)); +} + +module.exports = isFinite; \ No newline at end of file diff --git a/lib/util/isFunction.js b/lib/util/isFunction.js new file mode 100644 index 0000000..56c50b7 --- /dev/null +++ b/lib/util/isFunction.js @@ -0,0 +1,5 @@ +function isFunction(value) { + return typeof value == 'function'; +} + +module.exports = isFunction; \ No newline at end of file diff --git a/lib/util/isNaN.js b/lib/util/isNaN.js new file mode 100644 index 0000000..c8fc09d --- /dev/null +++ b/lib/util/isNaN.js @@ -0,0 +1,7 @@ +var isNumber = require('./isNumber'); + +function isNaN(value) { + return isNumber(value) && value != +value +} + +module.exports = isNaN; \ No newline at end of file diff --git a/lib/util/isNumber.js b/lib/util/isNumber.js new file mode 100644 index 0000000..c5ba353 --- /dev/null +++ b/lib/util/isNumber.js @@ -0,0 +1,8 @@ +var toString = require('./common').toString; +var numberClass = '[object Number]'; + +function isNumber(value) { + return typeof value == 'number' || toString.call(value) == numberClass; +} + +module.exports = isNumber; \ No newline at end of file diff --git a/lib/util/isObject.js b/lib/util/isObject.js new file mode 100644 index 0000000..5ff3a94 --- /dev/null +++ b/lib/util/isObject.js @@ -0,0 +1,7 @@ +var objectTypes = require('./common').objectTypes; + +function isObject(value) { + return value ? objectTypes[typeof value] : false; +} + +module.exports = isObject; \ No newline at end of file diff --git a/lib/util/keys.js b/lib/util/keys.js new file mode 100644 index 0000000..65019d6 --- /dev/null +++ b/lib/util/keys.js @@ -0,0 +1,25 @@ +var hasOwnProperty = require('./common').hasOwnProperty; +var nativeKeys = require('./common').nativeKeys; +var objectTypes = require('./common').objectTypes; + +var isObject = require('./isObject'); + +var shimKeys = function (object) { + var index, iterable = object, result = []; + if (!iterable) return result; + if (!(objectTypes[typeof object])) return result; + + for (index in iterable) { + if (hasOwnProperty.call(iterable, index)) { + result.push(index); + } + } + return result +}; + +module.exports = !nativeKeys ? shimKeys : function(object) { + if (!isObject(object)) { + return []; + } + return nativeKeys(object); +}; \ No newline at end of file diff --git a/lib/util/sortedIndex.js b/lib/util/sortedIndex.js new file mode 100644 index 0000000..e4cff4d --- /dev/null +++ b/lib/util/sortedIndex.js @@ -0,0 +1,20 @@ +var createCallback = require('./createCallback'); +var identity = require('./identity'); + +function sortedIndex(array, value, callback, thisArg) { + var low = 0, + high = array ? array.length : low; + + callback = callback ? createCallback(callback, thisArg, 1) : identity; + value = callback(value); + + while (low < high) { + var mid = (low + high) >>> 1; + (callback(array[mid]) < value) + ? low = mid + 1 + : high = mid; + } + return low; +} + +module.exports = sortedIndex; \ No newline at end of file diff --git a/package.json b/package.json index 3aa79a0..8a420d0 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "grunt-contrib-jshint": "~0.3.0", "grunt-contrib-uglify": "~0.2.0", "grunt-mocha": "~0.3.0", + "grunt-shell": "~0.2.0", "numbers": "~0.4.0", "testem": "~0.2.0" }, diff --git a/primality.js b/primality.js index fde8d1d..2c92d4a 100644 --- a/primality.js +++ b/primality.js @@ -7,258 +7,185 @@ * * Available under MIT license */ -(function(window) { +var primality; - // Detect free variable `exports` - var freeExports = typeof exports == 'object' && exports; +var WILSON_PRIMES = [5, 13, 563]; - /** Detect free variable `module` */ - var freeModule = typeof module == 'object' && module && module.exports == freeExports && module; +var _ = require('./lib/util/'); - // Detect free variable `global` and use it as `window` - var freeGlobal = typeof global == 'object' && global; - if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { - window = freeGlobal; - } - - var primality; - - var _ = null; - - // Try to import the _optional_ dependency Lo-Dash. - // If it is unavailable, replicate the API. - try { - _ = require('lodash'); - } catch (e) { - var numberClass = '[object Number]', - objectRef = Object(), - reNative = RegExp('^' + - String(objectRef.valueOf) - .replace(/[.*+?^${}()|[\]\\]/g, '\\$&') - .replace(/valueOf|for [^\]]+/g, '.+?') + '$' - ), - toString = objectRef.toString, - nativeIsArray = reNative.test(nativeIsArray = Array.isArray) && nativeIsArray, - nativeIsFinite = window.isFinite, - nativeIsNaN = window.isNaN - lodash = {}; - - var isArray = nativeIsArray; - - function isFinite(value) { - return nativeIsFinite(value) && !nativeIsNaN(parseFloat(value)); - } - - function isNumber(value) { - return typeof value == 'number' || toString.call(value) == numberClass; - } - - function isNaN(value) { - return isNumber(value) && value != +value; - } - - lodash.isArray = isArray; - lodash.isFinite = isFinite; - lodash.isNumber = isNumber; - lodash.isNaN = isNaN; - - _ = lodash; - } - - /** - * Finds the smallest factor of `n` - * - * @private - * @param {Number} value The value to check - * @returns {Number} - * The smallest prime that divides n - * NaN if n is NaN or Infinity - * 0 if n is 0 - * 1 if n = 1, n = -1, or n is not an integer - */ - function leastFactor(n) { - if (n === 0) return 0; - else if (n % 1 || n * n < 2) return 1; - else if (n % 2 === 0) return 2; - else if (n % 3 === 0) return 3; - else if (n % 5 === 0) return 5; - - var m = Math.sqrt(n); - for (var i = 7; i <= m; i += 30) { - if (n % i === 0) return i; - if (n % (i + 4) === 0) return i + 4; - if (n % (i + 6) === 0) return i + 6; - if (n % (i + 10) === 0) return i + 10; - if (n % (i + 12) === 0) return i + 12; - if (n % (i + 16) === 0) return i + 16; - if (n % (i + 22) === 0) return i + 22; - if (n % (i + 24) === 0) return i + 24; - } - return n; +/** + * Finds the smallest factor of `n` + * + * @private + * @param {Number} value The value to check + * @returns {Number} + * The smallest prime that divides n + * NaN if n is NaN or Infinity + * 0 if n is 0 + * 1 if n = 1, n = -1, or n is not an integer + */ +function leastFactor(n) { + if (n === 0) return 0; + else if (n % 1 || n * n < 2) return 1; + else if (n % 2 === 0) return 2; + else if (n % 3 === 0) return 3; + else if (n % 5 === 0) return 5; + + var m = Math.sqrt(n); + for (var i = 7; i <= m; i += 30) { + if (n % i === 0) return i; + if (n % (i + 4) === 0) return i + 4; + if (n % (i + 6) === 0) return i + 6; + if (n % (i + 10) === 0) return i + 10; + if (n % (i + 12) === 0) return i + 12; + if (n % (i + 16) === 0) return i + 16; + if (n % (i + 22) === 0) return i + 22; + if (n % (i + 24) === 0) return i + 24; } + return n; +} - /** - * Checks if `value` is prime. - * - * @private - * @param {Number} value The value to check - * @returns {Boolean} Returns `true` if `value` is prime - */ - function isPrime(value) { - if (_.isNaN(value) || !_.isFinite(value) || value % 1 || value < 2) { - return false; - } - if (value !== leastFactor(value)) return false; - return true; +/** + * Checks if `value` is prime. + * + * @private + * @param {Number} value The value to check + * @returns {Boolean} Returns `true` if `value` is prime + */ +function isPrime(value) { + if (_.isNaN(value) || !_.isFinite(value) || value % 1 || value < 2) { + return false; } + if (value !== leastFactor(value)) return false; + return true; +} - /** - * Creates a new primality instance. - * - * @name primality - * @constructor - * @param {Mixed} input A number, string, or array to check the primality of. - * @returns {Boolean} Returns `true` if `input` is prime. - * @example - * - * primality(7); - * // => true - * - * primality('13'); - * // => true - * - * primality([17, 19, 23]); - * // => true - */ - primality = function(input) { - if (input === null || input === '') return null; - else if (_.isArray(input)) { - for (var i = 0, l = input.length; i < l; i++) { - if (!isPrime(input[i])) return false; - } - return true; +/** + * Creates a new primality instance. + * + * @name primality + * @constructor + * @param {Mixed} input A number, string, or array to check the primality of. + * @returns {Boolean} Returns `true` if `input` is prime. + * @example + * + * primality(7); + * // => true + * + * primality('13'); + * // => true + * + * primality([17, 19, 23]); + * // => true + */ +primality = function(input) { + if (input === null || input === '') return null; + else if (_.isArray(input)) { + for (var i = 0, l = input.length; i < l; i++) { + if (!isPrime(input[i])) return false; } - else return isPrime(input); - }; - - /** - * Checks if `a` and `b` are twin primes - * - * - * - * @static - * @memberOf primality - * @param {Number} a First of the pair - * @param {Number} b Second of the pair - * @returns {Boolean} Returns `true` if `a` and `b` are twin primes - * @example - * - * primality.areTwinPrimes(3, 5) - * // => true - */ - function areTwinPrimes(a, b) { - if (Math.abs(a - b) != 2) return false; - if (!primality([a, b])) return false; - return true; - } - - /** - * Checks if `a` and `b` are cousin primes - * - * - * - * @static - * @memberOf primality - * @param {Number} a First of the pair - * @param {Number} b Second of the pair - * @returns {Boolean} Returns `true` if `a` and `b` are cousin primes - * @example - * - * primality.areCousinPrimes(3, 7) - * // => true - */ - function areCousinPrimes(a, b) { - if (Math.abs(a - b) != 4) return false; - if (!primality([a, b])) return false; return true; } + else return isPrime(input); +}; - /** - * Checks if `a` and `b` are sexy primes - * - * - * - * @static - * @memberOf primality - * @param {Number} a First of the pair - * @param {Number} b Second of the pair - * @returns {Boolean} Returns `true` if `a` and `b` are sexy primes - * @example - * - * primality.areSexyPrimes(5, 11) - * // => true - */ - function areSexyPrimes(a, b) { - if (Math.abs(a - b) != 6) return false; - if (!primality([a, b])) return false; +/** + * Checks if `a` and `b` are twin primes + * + * + * + * @static + * @memberOf primality + * @param {Number} a First of the pair + * @param {Number} b Second of the pair + * @returns {Boolean} Returns `true` if `a` and `b` are twin primes + * @example + * + * primality.areTwinPrimes(3, 5) + * // => true + */ +function areTwinPrimes(a, b) { + if (Math.abs(a - b) != 2) return false; + if (!primality([a, b])) return false; + return true; +} + +/** + * Checks if `a` and `b` are cousin primes + * + * + * + * @static + * @memberOf primality + * @param {Number} a First of the pair + * @param {Number} b Second of the pair + * @returns {Boolean} Returns `true` if `a` and `b` are cousin primes + * @example + * + * primality.areCousinPrimes(3, 7) + * // => true + */ +function areCousinPrimes(a, b) { + if (Math.abs(a - b) != 4) return false; + if (!primality([a, b])) return false; + return true; +} + +/** + * Checks if `a` and `b` are sexy primes + * + * + * + * @static + * @memberOf primality + * @param {Number} a First of the pair + * @param {Number} b Second of the pair + * @returns {Boolean} Returns `true` if `a` and `b` are sexy primes + * @example + * + * primality.areSexyPrimes(5, 11) + * // => true + */ +function areSexyPrimes(a, b) { + if (Math.abs(a - b) != 6) return false; + if (!primality([a, b])) return false; + return true; +} + +/** + * Checks if `a` is a Wilson prime. + * + * + * + * @static + * @memberOf primality + * @param {Number} a + * @returns {Boolean} Returns `true` if `a` is a Wilson prime. + * @example + * + * primality.isWilsonPrime(5); + * // => true + */ +function isWilsonPrime(a) { + if (_.indexOf(WILSON_PRIMES, a) > -1) { return true; } + return false; +} - /** - * The semantic version number. - * - * @static - * @memberOf primality - * @type String - */ - primality.VERSION = '1.4.0'; - - primality.areTwinPrimes = areTwinPrimes; - primality.areCousinPrimes = areCousinPrimes; - primality.areSexyPrimes = areSexyPrimes; - - /** - * Expose Primality - * - * Some AMD build optimizers, like r.js, check for specific condition patterns - * like the following: - */ - if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) { - /** - * Expose Primality to the global object even when an AMD loader is present - * in case Primality was injected by a third-party script and not intended - * to be loaded as a module. - */ - window.primality = primality; - - /** - * Define as an anonymous module so, through path mapping, it can be - * referenced as anything else - */ - define(function() { - return primality; - }); - } - - /** - * Check for `exports` after `define` in case a build optimizer adds an - * `exports` object - */ - else if (freeExports && !freeExports.nodeType) { +/** + * The semantic version number. + * + * @static + * @memberOf primality + * @type String + */ +primality.VERSION = '1.4.0'; - // Node.js or RingoJS v0.8.0+ - if (freeModule) { - (module.exports = primality).primality = primality; - } +primality.areTwinPrimes = areTwinPrimes; +primality.areCousinPrimes = areCousinPrimes; +primality.areSexyPrimes = areSexyPrimes; +primality.isWilsonPrime = isWilsonPrime; - // Narwhal or RingoJS v0.7.0- - else { - freeExports.primality = primality; - } - } - - // Browser or Rhino - else { - window.primality = primality; - } -}(this)); +// Expose Primality +(module.exports = primality).primality = primality; \ No newline at end of file diff --git a/primality.min.js b/primality.min.js index aa2fb0c..33df396 100644 --- a/primality.min.js +++ b/primality.min.js @@ -6,4 +6,4 @@ * (c) 2012 John-David Dalton * * Available under MIT license - */(function(r){function e(r){return h(r)&&!v(parseFloat(r))}function t(r){return"number"==typeof r||j.call(r)==m}function n(r){return t(r)&&r!=+r}function i(r){if(0===r)return 0;if(r%1||r*r<2)return 1;if(0===r%2)return 2;if(0===r%3)return 3;if(0===r%5)return 5;for(var e=Math.sqrt(r),t=7;t<=e;t+=30){if(0===r%t)return t;if(0===r%(t+4))return t+4;if(0===r%(t+6))return t+6;if(0===r%(t+10))return t+10;if(0===r%(t+12))return t+12;if(0===r%(t+16))return t+16;if(0===r%(t+22))return t+22;if(0===r%(t+24))return t+24}return r}function u(r){return p.isNaN(r)||!p.isFinite(r)||r%1||r<2?!1:r!==i(r)?!1:!0}function a(r,e){return 2!=Math.abs(r-e)?!1:c([r,e])?!0:!1}function o(r,e){return 4!=Math.abs(r-e)?!1:c([r,e])?!0:!1}function f(r,e){return 6!=Math.abs(r-e)?!1:c([r,e])?!0:!1}var s="object"==typeof global&&global;(s.global===s||s.window===s)&&(r=s);var c,l="object"==typeof exports&&exports,p=null;try{p=require("lodash")}catch(b){var y="[object Array]",m="[object Number]",d={},g=RegExp("^"+(d.valueOf+"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/valueOf|for [^\]]+/g,".+?")+"$"),j=d.toString,N=g.test(N=Array.isArray)&&N,h=r.isFinite,v=r.isNaN,x={},A=x.support={};A.argsObject=arguments.constructor==Object;var O=N||function(r){return A.argsObject&&r instanceof Array||j.call(r)==y};x.isArray=O,x.isFinite=e,x.isNumber=t,x.isNaN=n,p=x}c=function(r){if(null===r||""===r)return null;if(p.isArray(r)){for(var e=0,t=r.length;e-1?!0:!1}var primality,WILSON_PRIMES=[5,13,563],_=require("./lib/util/");primality=function(r){if(null===r||""===r)return null;if(_.isArray(r)){for(var i=0,e=r.length;i