-
Notifications
You must be signed in to change notification settings - Fork 1
/
primality.js
181 lines (171 loc) · 4.26 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
import factorial from 'factorial';
import isFinite from 'lodash.isfinite';
const WILSON_PRIMES = [5, 13, 563];
const WIEFERICH_PRIMES = [1093, 3511];
/**
* Finds the smallest factor of `n`
*
* @private
* @param {number} n 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;
const m = Math.sqrt(n);
for (let 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 | string} value The value to check
* @returns {boolean} Returns `true` if `value` is prime
*/
function isPrime(value) {
if (Number.isNaN(value) || !isFinite(value) || value % 1 || value < 2) {
return false;
}
if (value !== leastFactor(value)) return false;
return true;
}
/**
* Primality test.
*
* @param {number | string | readonly (number | string)[]} 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
*/
export default function primality(input) {
if (input === null || input === '') return null;
else if (Array.isArray(input)) {
let i = input.length;
while (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>
*
* @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
*/
export function areTwinPrimes(a, b) {
return isRelated(a, b, 2);
}
/**
* Checks if `a` and `b` are cousin primes
*
* <https://en.wikipedia.org/wiki/Cousin_prime>
*
* @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
*/
export function areCousinPrimes(a, b) {
return isRelated(a, b, 4);
}
/**
* Checks if `a` and `b` are sexy primes
*
* <https://en.wikipedia.org/wiki/Sexy_prime>
*
* @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
*/
export function areSexyPrimes(a, b) {
return isRelated(a, b, 6);
}
/**
* Checks if `value` is a Wilson prime.
*
* <https://en.wikipedia.org/wiki/Wilson_prime>
*
* @param {Number} value
* @returns {Boolean} Returns `true` if `value` is a Wilson prime.
* @example
*
* primality.isWilsonPrime(5);
* // => true
*/
export function isWilsonPrime(value) {
return WILSON_PRIMES.includes(value)
|| (factorial(value - 1, { useBigInt: true }) + 1n)
% BigInt(Math.pow(value, 2)) === 0;
}
/**
* Checks if `value` is a Wieferich prime.
*
* <https://en.wikipedia.org/wiki/Wieferich_prime>
*
* @param {Number} value
* @returns {Boolean} Returns `true` if `value` is a Wieferich prime.
* @example
*
* primality.isWieferichPrime(1093);
* // => true
*/
export function isWieferichPrime(value) {
return WIEFERICH_PRIMES.includes(value)
|| (Math.pow(2, value - 1) - 1) % Math.pow(value, 2) === 0;
}