-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprimality.js
215 lines (201 loc) · 4.84 KB
/
primality.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*!
* primality v1.5.8
* (c) 2012–2013 Kenan Yildirim
*
* Includes functions from Lo-Dash
* (c) 2012–2013 The Dojo Foundation
*
* Available under MIT license
*/
var primality;
/**
* "Hack" to use npm modules as components when they don't have a
* `component.json`. This prevents two annoyances:
*
* - bugging the developer to make a `component.json`
* - forking, adding `component.json`, and maintaining this fork
*
* First, a regular `require()` is attempted. If that fails, then we must be in
* the browser.
*/
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`
*
* @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;
else if (n % (i + 4) === 0) return i + 4;
else if (n % (i + 6) === 0) return i + 6;
else if (n % (i + 10) === 0) return i + 10;
else if (n % (i + 12) === 0) return i + 12;
else if (n % (i + 16) === 0) return i + 16;
else if (n % (i + 22) === 0) return i + 22;
else 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 primes which differ by `difference`.
*
* @private
* @param {Number} a First of the pair
* @param {Number} b Second of the pair
* @param {Number} difference
* @returns {Boolean}
*/
function isRelated(a, b, difference) {
return Math.abs(a - b) !== difference
? false
: primality([a, b]);
}
/**
* Checks if `a` and `b` are twin primes
*
* <https://en.wikipedia.org/wiki/Twin_prime>
*
* @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) {
return isRelated(a, b, 2);
}
/**
* Checks if `a` and `b` are cousin primes
*
* <https://en.wikipedia.org/wiki/Cousin_prime>
*
* @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) {
return isRelated(a, b, 4);
}
/**
* Checks if `a` and `b` are sexy primes
*
* <https://en.wikipedia.org/wiki/Sexy_prime>
*
* @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) {
return isRelated(a, b, 6);
}
/**
* Checks if `value` is a Wilson prime.
*
* <https://en.wikipedia.org/wiki/Wilson_prime>
*
* @static
* @memberOf primality
* @param {Number} value
* @returns {Boolean} Returns `true` if `value` is a Wilson prime.
* @example
*
* primality.isWilsonPrime(5);
* // => true
*/
function isWilsonPrime(value) {
return _.contains(WILSON_PRIMES, value)
? true
: (factorial(value - 1) + 1) % Math.pow(value, 2) === 0;
}
/**
* The semantic version number.
*
* @static
* @memberOf primality
* @type String
*/
primality.VERSION = '1.5.8';
primality.areTwinPrimes = areTwinPrimes;
primality.areCousinPrimes = areCousinPrimes;
primality.areSexyPrimes = areSexyPrimes;
primality.isWilsonPrime = isWilsonPrime;
// Expose Primality
(module.exports = primality).primality = primality;