Skip to content

Commit 81ca672

Browse files
committed
Update README.
1 parent b940932 commit 81ca672

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* [Fibonacci Number](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fibonacci)
3232
* [Cartesian Product](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/cartesian-product)
3333
* [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)
3435
* Collatz Conjecture algorithm
3536
* Extended Euclidean algorithm
3637
* Euclidean algorithm to calculate the Greatest Common Divisor (GCD)
@@ -39,7 +40,6 @@
3940
* Greatest Difference
4041
* Least Common Multiple
4142
* Newton's square
42-
* Primality Test
4343
* Shannon Entropy
4444
* **String**
4545
* [String Permutations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/permutations)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

0 commit comments

Comments
 (0)