diff --git a/src/algorithms/math/primality-test/README.md b/src/algorithms/math/primality-test/README.md index fd5af656..8a9852dc 100644 --- a/src/algorithms/math/primality-test/README.md +++ b/src/algorithms/math/primality-test/README.md @@ -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) diff --git a/src/algorithms/math/primality-test/__test__/trialDivision.test.js b/src/algorithms/math/primality-test/__test__/trialDivision.test.js index d4f54d47..26080857 100644 --- a/src/algorithms/math/primality-test/__test__/trialDivision.test.js +++ b/src/algorithms/math/primality-test/__test__/trialDivision.test.js @@ -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', () => { diff --git a/src/algorithms/math/primality-test/trialDivision.js b/src/algorithms/math/primality-test/trialDivision.js index 64e62472..f4d7c2b6 100644 --- a/src/algorithms/math/primality-test/trialDivision.js +++ b/src/algorithms/math/primality-test/trialDivision.js @@ -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;