mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
Fix bug with primality test.
This commit is contained in:
parent
9f83862212
commit
6c892c4b2f
@ -1,6 +1,16 @@
|
|||||||
# Primality Test
|
# 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
|
number is prime. Among other fields of mathematics, it is used
|
||||||
for cryptography. Unlike integer factorization, primality tests
|
for cryptography. Unlike integer factorization, primality tests
|
||||||
do not generally give prime factors, only stating whether the
|
do not generally give prime factors, only stating whether the
|
||||||
@ -11,4 +21,5 @@ size of the input).
|
|||||||
|
|
||||||
## References
|
## 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)
|
||||||
|
@ -23,6 +23,11 @@ function primalityTest(testFunction) {
|
|||||||
expect(testFunction(192)).toBeFalsy();
|
expect(testFunction(192)).toBeFalsy();
|
||||||
expect(testFunction(200)).toBeFalsy();
|
expect(testFunction(200)).toBeFalsy();
|
||||||
expect(testFunction(400)).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', () => {
|
describe('trialDivision', () => {
|
||||||
|
@ -3,6 +3,11 @@
|
|||||||
* @return {boolean}
|
* @return {boolean}
|
||||||
*/
|
*/
|
||||||
export default function trialDivision(number) {
|
export default function trialDivision(number) {
|
||||||
|
// Check if number is integer.
|
||||||
|
if (number % 1 !== 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (number <= 1) {
|
if (number <= 1) {
|
||||||
// If number is less than one then it isn't prime by definition.
|
// If number is less than one then it isn't prime by definition.
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user