Skip to content

Commit

Permalink
Use npm package factorial and simplify modulus
Browse files Browse the repository at this point in the history
  • Loading branch information
kenany committed Aug 25, 2013
1 parent cf6ab50 commit 2c5b798
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 65 deletions.
3 changes: 2 additions & 1 deletion component.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"lib/util/contains.js",
"lib/util/isArray.js",
"lib/util/isFinite.js",
"lib/util/isNaN.js"
"lib/util/isNaN.js",
"node_modules/factorial/index.js"
],
"license": "MIT"
}
33 changes: 13 additions & 20 deletions dist/primality.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,14 @@
};
require.register("primality/primality.js", function(exports, require, module) {
var primality;
var WILSON_PRIMES = [ 5, 13, 563 ];
var _ = require("./lib/util/");
function factorial(value) {
return value === 0 ? 1 : value * factorial(value - 1);
}
function mod(x, y) {
if (y > 0) {
if (x > 0) {
return x % y;
} else if (x === 0) {
return 0;
} else {
return x - y * Math.floor(x / y);
}
} else if (y === 0) {
return x;
} else {
throw new Error("Cannot calculate mod for a negative divisor");
}
var factorial;
try {
factorial = require("factorial");
} catch (e) {
factorial = require("./node_modules/factorial/");
}
var _ = require("./lib/util/");
var WILSON_PRIMES = [ 5, 13, 563 ];
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);
Expand Down Expand Up @@ -147,7 +135,7 @@
return isRelated(a, b, 6);
}
function isWilsonPrime(value) {
return _.contains(WILSON_PRIMES, value) ? true : mod(factorial(value - 1) + 1, Math.pow(value, 2)) === 0;
return _.contains(WILSON_PRIMES, value) ? true : (factorial(value - 1) + 1) % Math.pow(value, 2) === 0;
}
primality.VERSION = "1.5.7";
primality.areTwinPrimes = areTwinPrimes;
Expand Down Expand Up @@ -208,6 +196,11 @@
}
module.exports = isNaN;
});
require.register("primality/node_modules/factorial/index.js", function(exports, require, module) {
module.exports = function factorial(v) {
return v === 0 ? 1 : v * factorial(v - 1);
};
});
require.alias("primality/primality.js", "primality/index.js");
var objectTypes = {
"boolean": false,
Expand Down
12 changes: 6 additions & 6 deletions dist/primality.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"test": "grunt test",
"bench": "node perf/index.js"
},
"dependencies": {
"factorial": "git://github.com/wearefractal/factorial.git#0ec466f15043e3cf7bb115f3f21b27fda3f3c55b"
},
"devDependencies": {
"async": "~0.2.0",
"benchmark": "~1.0.0",
Expand Down
54 changes: 16 additions & 38 deletions primality.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,27 @@
*/
var primality;

var WILSON_PRIMES = [5, 13, 563];

var _ = require('./lib/util/');

/**
* Returns the factorial of `value`.
* "Hack" to use npm modules as components when they don't have a
* `component.json`. This prevents two annoyances:
*
* @private
* @param {Number} value
* @returns {Number} The factorial of `value`.
*/
function factorial(value) {
return value === 0 ? 1 : value * factorial(value - 1);
}

/**
* Returns the modulus of two numbers.
* - bugging the developer to make a `component.json`
* - forking, adding `component.json`, and maintaining this fork
*
* @private
* @param {Number} x
* @param {Number} y
* @returns {Number}
* First, a regular `require()` is attempted. If that fails, then we must be in
* the browser.
*/
function mod(x, y) {
if (y > 0) {
if (x > 0) {
return x % y;
}
else if (x === 0) {
return 0;
}
else {
return x - y * Math.floor(x / y);
}
}
else if (y === 0) {
return x;
}
else {
throw new Error('Cannot calculate mod for a negative divisor');
}
var factorial;
try {
factorial = require('factorial');
} catch (e) {
factorial = require('./node_modules/factorial/');
}

var _ = require('./lib/util/');

var WILSON_PRIMES = [5, 13, 563];

/**
* Finds the smallest factor of `n`
*
Expand Down Expand Up @@ -216,7 +194,7 @@ function areSexyPrimes(a, b) {
function isWilsonPrime(value) {
return _.contains(WILSON_PRIMES, value)
? true
: mod(factorial(value - 1) + 1, Math.pow(value, 2)) === 0;
: (factorial(value - 1) + 1) % Math.pow(value, 2) === 0;
}

/**
Expand Down

0 comments on commit 2c5b798

Please sign in to comment.