1 | import { deepMap } from '../../utils/collection.js';
|
2 | import { factory } from '../../utils/factory.js';
|
3 | var name = 'isPrime';
|
4 | var dependencies = ['typed'];
|
5 | export var createIsPrime = /* #__PURE__ */factory(name, dependencies, (_ref) => {
|
6 | var {
|
7 | typed
|
8 | } = _ref;
|
9 |
|
10 | /**
|
11 | * Test whether a value is prime: has no divisors other than itself and one.
|
12 | * The function supports type `number`, `bignumber`.
|
13 | *
|
14 | * The function is evaluated element-wise in case of Array or Matrix input.
|
15 | *
|
16 | * Syntax:
|
17 | *
|
18 | * math.isPrime(x)
|
19 | *
|
20 | * Examples:
|
21 | *
|
22 | * math.isPrime(3) // returns true
|
23 | * math.isPrime(-2) // returns false
|
24 | * math.isPrime(0) // returns false
|
25 | * math.isPrime(-0) // returns false
|
26 | * math.isPrime(0.5) // returns false
|
27 | * math.isPrime('2') // returns true
|
28 | * math.isPrime([2, 17, 100]) // returns [true, true, false]
|
29 | *
|
30 | * See also:
|
31 | *
|
32 | * isNumeric, isZero, isNegative, isInteger
|
33 | *
|
34 | * @param {number | BigNumber | Array | Matrix} x Value to be tested
|
35 | * @return {boolean} Returns true when `x` is larger than zero.
|
36 | * Throws an error in case of an unknown data type.
|
37 | */
|
38 | return typed(name, {
|
39 | number: function number(x) {
|
40 | if (x * 0 !== 0) {
|
41 | return false;
|
42 | }
|
43 |
|
44 | if (x <= 3) {
|
45 | return x > 1;
|
46 | }
|
47 |
|
48 | if (x % 2 === 0 || x % 3 === 0) {
|
49 | return false;
|
50 | }
|
51 |
|
52 | for (var i = 5; i * i <= x; i += 6) {
|
53 | if (x % i === 0 || x % (i + 2) === 0) {
|
54 | return false;
|
55 | }
|
56 | }
|
57 |
|
58 | return true;
|
59 | },
|
60 | BigNumber: function BigNumber(n) {
|
61 | if (n.toNumber() * 0 !== 0) {
|
62 | return false;
|
63 | }
|
64 |
|
65 | if (n.lte(3)) return n.gt(1);
|
66 | if (n.mod(2).eq(0) || n.mod(3).eq(0)) return false;
|
67 |
|
68 | for (var i = 5; n.gte(i * i); i += 6) {
|
69 | if (n.mod(i).eq(0) || n.mod(i + 2).eq(0)) {
|
70 | return false;
|
71 | }
|
72 | }
|
73 |
|
74 | return true;
|
75 | },
|
76 | 'Array | Matrix': function ArrayMatrix(x) {
|
77 | return deepMap(x, this);
|
78 | }
|
79 | });
|
80 | }); |
\ | No newline at end of file |