1- exports . isPrime = ( ...params ) => {
2- if ( params . length === 0 ) throw new Error ( 'Must provide one or more paramters' ) ;
3- if ( params . some ( param => Number . isNaN ( Number ( param ) ) ) ) throw new Error ( 'One of your parameters does not evaluate to a number' ) ;
4- // JS can only safely represent and compare integers
5- // up to Number.MAX_SAFE_INTEGER: i.e., 9,007,199,254,740,991.
6- // This also covers checking if any of the parameters evaluates to Infinity
7- if ( params . some ( param => param > Number . MAX_SAFE_INTEGER ) ) {
8- throw new Error ( 'Cannot reliably test primality of numbers larger than 9,007,199,254,740,991' ) ;
9- }
10- return params . every ( testPrime ) ;
11- } ;
12- // This method tests the primality of a number by trial division.
13- // In other words, it attempts to divide the test number by a series integers.
14- // If any of those integers turn out to be factors of the test number,
15- // then we know the test number is composite: i.e., not prime.
16- // the most brute force method is to simply divide the test number by every integer
17- // smaller than itself, but we can use our knowledge of number properties to greatly
18- // reduce the list of potential factors that we need to test against.
19- const testPrime = ( num ) => {
20- // num % 1 checks that num is an integer.
21- // There are no prime numbers less than 2, hence check for num < 2.
22- if ( num % 1 || num < 2 ) return false ;
23- // if the least factor of num is itself (aside from 1), then num is prime.
24- if ( num === leastFactor ( num ) ) return true ;
25- return false ;
26- } ;
27-
281const leastFactor = ( num ) => {
292 // Check if 2, 3, or 5 is a factor of num AND
303 // eliminate from future consideration all possible factors that are multiples of 2, 3, and 5.
@@ -46,3 +19,31 @@ const leastFactor = (num) => {
4619 }
4720 return num ;
4821} ;
22+
23+ // This method tests the primality of a number by trial division.
24+ // In other words, it attempts to divide the test number by a series integers.
25+ // If any of those integers turn out to be factors of the test number,
26+ // then we know the test number is composite: i.e., not prime.
27+ // the most brute force method is to simply divide the test number by every integer
28+ // smaller than itself, but we can use our knowledge of number properties to greatly
29+ // reduce the list of potential factors that we need to test against.
30+ const testPrime = ( num ) => {
31+ // num % 1 checks that num is an integer.
32+ // There are no prime numbers less than 2, hence check for num < 2.
33+ if ( num % 1 || num < 2 ) return false ;
34+ // if the least factor of num is itself (aside from 1), then num is prime.
35+ if ( num === leastFactor ( num ) ) return true ;
36+ return false ;
37+ } ;
38+
39+ exports . isPrime = ( ...params ) => {
40+ if ( params . length === 0 ) throw new Error ( 'Must provide one or more paramters' ) ;
41+ if ( params . some ( param => Number . isNaN ( Number ( param ) ) ) ) throw new Error ( 'One of your parameters does not evaluate to a number' ) ;
42+ // JS can only safely represent and compare integers
43+ // up to Number.MAX_SAFE_INTEGER: i.e., 9,007,199,254,740,991.
44+ // This also covers checking if any of the parameters evaluates to Infinity
45+ if ( params . some ( param => param > Number . MAX_SAFE_INTEGER ) ) {
46+ throw new Error ( 'Cannot reliably test primality of numbers larger than 9,007,199,254,740,991' ) ;
47+ }
48+ return params . every ( testPrime ) ;
49+ } ;
0 commit comments