Skip to content

Commit

Permalink
isWilsonPrime now works with unknown Wilson primes
Browse files Browse the repository at this point in the history
  • Loading branch information
kenany committed Aug 11, 2013
1 parent 5b45fb8 commit 7a7120f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 16 deletions.
24 changes: 21 additions & 3 deletions dist/primality.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* primality v1.5.5
* primality v1.5.6
* (c) 2012–2013 Kenan Yildirim
*
* Includes functions from Lo-Dash
Expand Down Expand Up @@ -93,6 +93,24 @@
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");
}
}
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 @@ -138,8 +156,8 @@
if (!primality([ a, b ])) return false;
return true;
}
function isWilsonPrime(a) {
return _.contains(WILSON_PRIMES, a);
function isWilsonPrime(value) {
return _.contains(WILSON_PRIMES, value) ? true : 0 === (mod(factorial(value - 1) + 1, value) === 0);
}
primality.VERSION = "1.4.0";
primality.areTwinPrimes = areTwinPrimes;
Expand Down
15 changes: 8 additions & 7 deletions dist/primality.min.js

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

51 changes: 46 additions & 5 deletions primality.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,45 @@ var WILSON_PRIMES = [5, 13, 563];

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

/**
* Returns the factorial of `value`
*
* @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.
*
* @param {Number} x
* @param {Number} y
* @returns {number} res
* @private
*/
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');
}
}

/**
* Finds the smallest factor of `n`
*
Expand Down Expand Up @@ -153,21 +192,23 @@ function areSexyPrimes(a, b) {
}

/**
* Checks if `a` is a Wilson prime.
* Checks if `value` is a Wilson prime.
*
* <https://en.wikipedia.org/wiki/Wilson_prime>
*
* @static
* @memberOf primality
* @param {Number} a
* @returns {Boolean} Returns `true` if `a` is a Wilson prime.
* @param {Number} value
* @returns {Boolean} Returns `true` if `value` is a Wilson prime.
* @example
*
* primality.isWilsonPrime(5);
* // => true
*/
function isWilsonPrime(a) {
return _.contains(WILSON_PRIMES, a);
function isWilsonPrime(value) {
return _.contains(WILSON_PRIMES, value)
? true
: 0 === (mod(factorial(value - 1) + 1, value) === 0);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/spec/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ define(['../../dist/primality'], function(primality) {
primality.isWilsonPrime(5).should.equal(true);
primality.isWilsonPrime(13).should.equal(true);
primality.isWilsonPrime(563).should.equal(true);
primality.isWilsonPrime(564).should.equal(false);
primality.isWilsonPrime(1000).should.equal(false);
});
});
});
Expand Down

0 comments on commit 7a7120f

Please sign in to comment.