File tree Expand file tree Collapse file tree 5 files changed +73
-1
lines changed
src/algorithms/math/primality-tests Expand file tree Collapse file tree 5 files changed +73
-1
lines changed Original file line number Diff line number Diff line change 31
31
* [ Fibonacci Number] ( https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fibonacci )
32
32
* [ Cartesian Product] ( https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/cartesian-product )
33
33
* [ Power Set] ( https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/power-set )
34
+ * [ Primality Tests] ( https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/primality-tests )
34
35
* Collatz Conjecture algorithm
35
36
* Extended Euclidean algorithm
36
37
* Euclidean algorithm to calculate the Greatest Common Divisor (GCD)
39
40
* Greatest Difference
40
41
* Least Common Multiple
41
42
* Newton's square
42
- * Primality Test
43
43
* Shannon Entropy
44
44
* ** String**
45
45
* [ String Permutations] ( https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/permutations )
Original file line number Diff line number Diff line change
1
+ # Primality Test
2
+
3
+ A primality test is an algorithm for determining whether an input
4
+ number is prime. Among other fields of mathematics, it is used
5
+ for cryptography. Unlike integer factorization, primality tests
6
+ do not generally give prime factors, only stating whether the
7
+ input number is prime or not. Factorization is thought to be
8
+ a computationally difficult problem, whereas primality testing
9
+ is comparatively easy (its running time is polynomial in the
10
+ size of the input).
11
+
12
+ ## References
13
+
14
+ [ Wikipedia] ( https://en.wikipedia.org/wiki/Primality_test )
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {function(n: number) } testFunction
3
+ */
4
+ export default function primalityTest ( testFunction ) {
5
+ expect ( testFunction ( 1 ) ) . toBeTruthy ( ) ;
6
+ expect ( testFunction ( 2 ) ) . toBeTruthy ( ) ;
7
+ expect ( testFunction ( 3 ) ) . toBeTruthy ( ) ;
8
+ expect ( testFunction ( 5 ) ) . toBeTruthy ( ) ;
9
+ expect ( testFunction ( 11 ) ) . toBeTruthy ( ) ;
10
+ expect ( testFunction ( 191 ) ) . toBeTruthy ( ) ;
11
+ expect ( testFunction ( 191 ) ) . toBeTruthy ( ) ;
12
+ expect ( testFunction ( 199 ) ) . toBeTruthy ( ) ;
13
+
14
+ expect ( testFunction ( - 1 ) ) . toBeFalsy ( ) ;
15
+ expect ( testFunction ( 0 ) ) . toBeFalsy ( ) ;
16
+ expect ( testFunction ( 4 ) ) . toBeFalsy ( ) ;
17
+ expect ( testFunction ( 6 ) ) . toBeFalsy ( ) ;
18
+ expect ( testFunction ( 12 ) ) . toBeFalsy ( ) ;
19
+ expect ( testFunction ( 192 ) ) . toBeFalsy ( ) ;
20
+ expect ( testFunction ( 200 ) ) . toBeFalsy ( ) ;
21
+ expect ( testFunction ( 400 ) ) . toBeFalsy ( ) ;
22
+ }
Original file line number Diff line number Diff line change
1
+ import trialDivision from '../trialDivision' ;
2
+ import primalityTest from './primalityTest' ;
3
+
4
+ describe ( 'trialDivision' , ( ) => {
5
+ it ( 'should detect prime numbers' , ( ) => {
6
+ primalityTest ( trialDivision ) ;
7
+ } ) ;
8
+ } ) ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } number
3
+ * @return {boolean }
4
+ */
5
+ export default function trialDivision ( number ) {
6
+ if ( number <= 0 ) {
7
+ // If number is less then one then it isn't prime by definition.
8
+ return false ;
9
+ } else if ( number <= 3 ) {
10
+ // All numbers from 1 to 3 are prime.
11
+ return true ;
12
+ }
13
+
14
+ // If the number is not divided by 2 then we may eliminate all further even dividers.
15
+ if ( number % 2 === 0 ) {
16
+ return false ;
17
+ }
18
+
19
+ // If there is no dividers up to square root of n then there is no higher dividers as well.
20
+ const dividerLimit = Math . sqrt ( number ) ;
21
+ for ( let divider = 3 ; divider < dividerLimit ; divider += 2 ) {
22
+ if ( number % divider === 0 ) {
23
+ return false ;
24
+ }
25
+ }
26
+
27
+ return true ;
28
+ }
You can’t perform that action at this time.
0 commit comments