Skip to content

Commit 6c892c4

Browse files
committedJun 5, 2018
Fix bug with primality test.
1 parent 9f83862 commit 6c892c4

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed
 

‎src/algorithms/math/primality-test/README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Primality Test
22

3-
A primality test is an algorithm for determining whether an input
3+
A **prime number** (or a **prime**) is a natural number greater than `1` that
4+
cannot be formed by multiplying two smaller natural numbers. A natural number
5+
greater than `1` that is not prime is called a composite number. For
6+
example, `5` is prime because the only ways of writing it as a
7+
product, `1 × 5` or `5 × 1`, involve `5` itself. However, `6` is
8+
composite because it is the product of two numbers `(2 × 3)` that are
9+
both smaller than `6`.
10+
11+
![Prime Numbers](https://upload.wikimedia.org/wikipedia/commons/f/f0/Primes-vs-composites.svg)
12+
13+
A **primality test** is an algorithm for determining whether an input
414
number is prime. Among other fields of mathematics, it is used
515
for cryptography. Unlike integer factorization, primality tests
616
do not generally give prime factors, only stating whether the
@@ -11,4 +21,5 @@ size of the input).
1121

1222
## References
1323

14-
[Wikipedia](https://en.wikipedia.org/wiki/Primality_test)
24+
- [Prime Numbers on Wikipedia](https://en.wikipedia.org/wiki/Prime_number)
25+
- [Primality Test on Wikipedia](https://en.wikipedia.org/wiki/Primality_test)

‎src/algorithms/math/primality-test/__test__/trialDivision.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ function primalityTest(testFunction) {
2323
expect(testFunction(192)).toBeFalsy();
2424
expect(testFunction(200)).toBeFalsy();
2525
expect(testFunction(400)).toBeFalsy();
26+
27+
// It should also deal with floats.
28+
expect(testFunction(0.5)).toBeFalsy();
29+
expect(testFunction(1.3)).toBeFalsy();
30+
expect(testFunction(10.5)).toBeFalsy();
2631
}
2732

2833
describe('trialDivision', () => {

‎src/algorithms/math/primality-test/trialDivision.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
* @return {boolean}
44
*/
55
export default function trialDivision(number) {
6+
// Check if number is integer.
7+
if (number % 1 !== 0) {
8+
return false;
9+
}
10+
611
if (number <= 1) {
712
// If number is less than one then it isn't prime by definition.
813
return false;

0 commit comments

Comments
 (0)
Please sign in to comment.