main.js (3885B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var sqrt = require( './../../../../base/special/sqrt' ); 24 var floor = require( './../../../../base/special/floor' ); 25 var FLOAT64_MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' ); 26 var WHEEL_PRIMES = require( './wheel_primes.json' ); 27 28 29 // MAIN // 30 31 /** 32 * Returns a boolean indicating whether a number is a prime. 33 * 34 * @param {number} x - value to test 35 * @returns {boolean} boolean indicating whether a value is a prime number 36 * 37 * @example 38 * var bool = isPrime( 5.0 ); 39 * // returns true 40 * 41 * @example 42 * var bool = isPrime( 4.0 ); 43 * // returns false 44 */ 45 function isPrime( x ) { 46 var N; 47 var i; 48 49 // Check whether the number is an integer... 50 if ( floor( x ) !== x ) { 51 return false; 52 } 53 // Check whether the number is positive... 54 if ( x <= 3 ) { 55 return (x > 1); // primes: 2, 3 56 } 57 // Check whether the number is even... 58 if ( x > FLOAT64_MAX_SAFE_INTEGER || x%2 === 0 ) { 59 return false; 60 } 61 // Check for small primes... 62 if ( x < 9 ) { 63 return true; // primes: 5, 7 64 } 65 // Check whether the number is evenly divisible by `3`... 66 if ( x%3 === 0 ) { 67 return false; 68 } 69 // Check whether the number is evenly divisible by `5`... 70 if ( x%5 === 0 ) { 71 return false; 72 } 73 // Check whether the number is evenly divisible by `7`... 74 if ( x%7 === 0 ) { 75 return false; 76 } 77 // Check whether the number is a prime number in the wheel... 78 if ( WHEEL_PRIMES[ x ] ) { 79 return true; 80 } 81 // Use trial division (with wheel factorization; see https://en.wikipedia.org/wiki/Wheel_factorization) to detect composite numbers, leveraging the fact that all primes greater than `210` are of the form `210k±1`... 82 N = floor( sqrt( x ) ); 83 for ( i = 11; i <= N; i += 210 ) { 84 if ( 85 x%i === 0 || // 11 86 x%(i+2) === 0 || // 13 87 x%(i+6) === 0 || // 17 88 x%(i+8) === 0 || // 19 89 x%(i+12) === 0 || // 23 90 x%(i+18) === 0 || // 29 91 x%(i+20) === 0 || // 31 92 x%(i+26) === 0 || // 37 93 x%(i+30) === 0 || // 41 94 x%(i+32) === 0 || // 43 95 x%(i+36) === 0 || // 47 96 x%(i+42) === 0 || // 53 97 x%(i+48) === 0 || // 59 98 x%(i+50) === 0 || // 61 99 x%(i+56) === 0 || // 67 100 x%(i+60) === 0 || // 71 101 x%(i+62) === 0 || // 73 102 x%(i+68) === 0 || // 79 103 x%(i+72) === 0 || // 83 104 x%(i+78) === 0 || // 89 105 x%(i+86) === 0 || // 97 106 x%(i+90) === 0 || // 101 107 x%(i+92) === 0 || // 103 108 x%(i+96) === 0 || // 107 109 x%(i+98) === 0 || // 109 110 x%(i+102) === 0 || // 113 111 x%(i+110) === 0 || // 121 (relatively prime) 112 x%(i+116) === 0 || // 127 113 x%(i+120) === 0 || // 131 114 x%(i+126) === 0 || // 137 115 x%(i+128) === 0 || // 139 116 x%(i+132) === 0 || // 143 (relatively prime) 117 x%(i+138) === 0 || // 149 118 x%(i+140) === 0 || // 151 119 x%(i+146) === 0 || // 157 120 x%(i+152) === 0 || // 163 121 x%(i+156) === 0 || // 167 122 x%(i+158) === 0 || // 169 (relatively prime) 123 x%(i+162) === 0 || // 173 124 x%(i+168) === 0 || // 179 125 x%(i+170) === 0 || // 181 126 x%(i+176) === 0 || // 187 (relatively prime) 127 x%(i+180) === 0 || // 191 128 x%(i+182) === 0 || // 193 129 x%(i+186) === 0 || // 197 130 x%(i+188) === 0 || // 199 131 x%(i+198) === 0 || // 209 (relatively prime) 132 x%(i+200) === 0 // 211 133 ) { 134 return false; 135 } 136 } 137 return true; 138 } 139 140 141 // EXPORTS // 142 143 module.exports = isPrime;