mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-07 01:44:52 +08:00
Add primality tests.
This commit is contained in:
@ -16,6 +16,8 @@ export default function primalityTest(testFunction) {
|
|||||||
expect(testFunction(4)).toBeFalsy();
|
expect(testFunction(4)).toBeFalsy();
|
||||||
expect(testFunction(6)).toBeFalsy();
|
expect(testFunction(6)).toBeFalsy();
|
||||||
expect(testFunction(12)).toBeFalsy();
|
expect(testFunction(12)).toBeFalsy();
|
||||||
|
expect(testFunction(14)).toBeFalsy();
|
||||||
|
expect(testFunction(25)).toBeFalsy();
|
||||||
expect(testFunction(192)).toBeFalsy();
|
expect(testFunction(192)).toBeFalsy();
|
||||||
expect(testFunction(200)).toBeFalsy();
|
expect(testFunction(200)).toBeFalsy();
|
||||||
expect(testFunction(400)).toBeFalsy();
|
expect(testFunction(400)).toBeFalsy();
|
||||||
|
@ -18,7 +18,7 @@ export default function trialDivision(number) {
|
|||||||
|
|
||||||
// If there is no dividers up to square root of n then there is no higher dividers as well.
|
// If there is no dividers up to square root of n then there is no higher dividers as well.
|
||||||
const dividerLimit = Math.sqrt(number);
|
const dividerLimit = Math.sqrt(number);
|
||||||
for (let divider = 3; divider < dividerLimit; divider += 2) {
|
for (let divider = 3; divider <= dividerLimit; divider += 2) {
|
||||||
if (number % divider === 0) {
|
if (number % divider === 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user