mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-07 18:10:24 +08:00
Update README.
This commit is contained in:
@ -31,6 +31,7 @@
|
|||||||
* [Fibonacci Number](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fibonacci)
|
* [Fibonacci Number](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fibonacci)
|
||||||
* [Cartesian Product](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/cartesian-product)
|
* [Cartesian Product](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/cartesian-product)
|
||||||
* [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/power-set)
|
* [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/power-set)
|
||||||
|
* [Primality Tests](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/primality-tests)
|
||||||
* Collatz Conjecture algorithm
|
* Collatz Conjecture algorithm
|
||||||
* Extended Euclidean algorithm
|
* Extended Euclidean algorithm
|
||||||
* Euclidean algorithm to calculate the Greatest Common Divisor (GCD)
|
* Euclidean algorithm to calculate the Greatest Common Divisor (GCD)
|
||||||
@ -39,7 +40,6 @@
|
|||||||
* Greatest Difference
|
* Greatest Difference
|
||||||
* Least Common Multiple
|
* Least Common Multiple
|
||||||
* Newton's square
|
* Newton's square
|
||||||
* Primality Test
|
|
||||||
* Shannon Entropy
|
* Shannon Entropy
|
||||||
* **String**
|
* **String**
|
||||||
* [String Permutations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/permutations)
|
* [String Permutations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/permutations)
|
||||||
|
14
src/algorithms/math/primality-tests/README.md
Normal file
14
src/algorithms/math/primality-tests/README.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# Primality Test
|
||||||
|
|
||||||
|
A primality test is an algorithm for determining whether an input
|
||||||
|
number is prime. Among other fields of mathematics, it is used
|
||||||
|
for cryptography. Unlike integer factorization, primality tests
|
||||||
|
do not generally give prime factors, only stating whether the
|
||||||
|
input number is prime or not. Factorization is thought to be
|
||||||
|
a computationally difficult problem, whereas primality testing
|
||||||
|
is comparatively easy (its running time is polynomial in the
|
||||||
|
size of the input).
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
[Wikipedia](https://en.wikipedia.org/wiki/Primality_test)
|
@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* @param {function(n: number)} testFunction
|
||||||
|
*/
|
||||||
|
export default function primalityTest(testFunction) {
|
||||||
|
expect(testFunction(1)).toBeTruthy();
|
||||||
|
expect(testFunction(2)).toBeTruthy();
|
||||||
|
expect(testFunction(3)).toBeTruthy();
|
||||||
|
expect(testFunction(5)).toBeTruthy();
|
||||||
|
expect(testFunction(11)).toBeTruthy();
|
||||||
|
expect(testFunction(191)).toBeTruthy();
|
||||||
|
expect(testFunction(191)).toBeTruthy();
|
||||||
|
expect(testFunction(199)).toBeTruthy();
|
||||||
|
|
||||||
|
expect(testFunction(-1)).toBeFalsy();
|
||||||
|
expect(testFunction(0)).toBeFalsy();
|
||||||
|
expect(testFunction(4)).toBeFalsy();
|
||||||
|
expect(testFunction(6)).toBeFalsy();
|
||||||
|
expect(testFunction(12)).toBeFalsy();
|
||||||
|
expect(testFunction(192)).toBeFalsy();
|
||||||
|
expect(testFunction(200)).toBeFalsy();
|
||||||
|
expect(testFunction(400)).toBeFalsy();
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
import trialDivision from '../trialDivision';
|
||||||
|
import primalityTest from './primalityTest';
|
||||||
|
|
||||||
|
describe('trialDivision', () => {
|
||||||
|
it('should detect prime numbers', () => {
|
||||||
|
primalityTest(trialDivision);
|
||||||
|
});
|
||||||
|
});
|
28
src/algorithms/math/primality-tests/trialDivision.js
Normal file
28
src/algorithms/math/primality-tests/trialDivision.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/**
|
||||||
|
* @param {number} number
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
|
export default function trialDivision(number) {
|
||||||
|
if (number <= 0) {
|
||||||
|
// If number is less then one then it isn't prime by definition.
|
||||||
|
return false;
|
||||||
|
} else if (number <= 3) {
|
||||||
|
// All numbers from 1 to 3 are prime.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the number is not divided by 2 then we may eliminate all further even dividers.
|
||||||
|
if (number % 2 === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is no dividers up to square root of n then there is no higher dividers as well.
|
||||||
|
const dividerLimit = Math.sqrt(number);
|
||||||
|
for (let divider = 3; divider < dividerLimit; divider += 2) {
|
||||||
|
if (number % divider === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
Reference in New Issue
Block a user