Fix bug with primality test.

This commit is contained in:
Oleksii Trekhleb 2018-06-05 21:50:08 +03:00
parent 9f83862212
commit 6c892c4b2f
3 changed files with 23 additions and 2 deletions

View File

@ -1,6 +1,16 @@
# Primality Test
A primality test is an algorithm for determining whether an input
A **prime number** (or a **prime**) is a natural number greater than `1` that
cannot be formed by multiplying two smaller natural numbers. A natural number
greater than `1` that is not prime is called a composite number. For
example, `5` is prime because the only ways of writing it as a
product, `1 × 5` or `5 × 1`, involve `5` itself. However, `6` is
composite because it is the product of two numbers `(2 × 3)` that are
both smaller than `6`.
![Prime Numbers](https://upload.wikimedia.org/wikipedia/commons/f/f0/Primes-vs-composites.svg)
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
@ -11,4 +21,5 @@ size of the input).
## References
[Wikipedia](https://en.wikipedia.org/wiki/Primality_test)
- [Prime Numbers on Wikipedia](https://en.wikipedia.org/wiki/Prime_number)
- [Primality Test on Wikipedia](https://en.wikipedia.org/wiki/Primality_test)

View File

@ -23,6 +23,11 @@ function primalityTest(testFunction) {
expect(testFunction(192)).toBeFalsy();
expect(testFunction(200)).toBeFalsy();
expect(testFunction(400)).toBeFalsy();
// It should also deal with floats.
expect(testFunction(0.5)).toBeFalsy();
expect(testFunction(1.3)).toBeFalsy();
expect(testFunction(10.5)).toBeFalsy();
}
describe('trialDivision', () => {

View File

@ -3,6 +3,11 @@
* @return {boolean}
*/
export default function trialDivision(number) {
// Check if number is integer.
if (number % 1 !== 0) {
return false;
}
if (number <= 1) {
// If number is less than one then it isn't prime by definition.
return false;