mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-07 01:44:52 +08:00
Don't treat 1 as prime number.
This commit is contained in:
@ -4,7 +4,7 @@ import trialDivision from '../trialDivision';
|
|||||||
* @param {function(n: number)} testFunction
|
* @param {function(n: number)} testFunction
|
||||||
*/
|
*/
|
||||||
function primalityTest(testFunction) {
|
function primalityTest(testFunction) {
|
||||||
expect(testFunction(1)).toBeTruthy();
|
expect(testFunction(1)).toBeFalsy();
|
||||||
expect(testFunction(2)).toBeTruthy();
|
expect(testFunction(2)).toBeTruthy();
|
||||||
expect(testFunction(3)).toBeTruthy();
|
expect(testFunction(3)).toBeTruthy();
|
||||||
expect(testFunction(5)).toBeTruthy();
|
expect(testFunction(5)).toBeTruthy();
|
||||||
|
@ -3,11 +3,11 @@
|
|||||||
* @return {boolean}
|
* @return {boolean}
|
||||||
*/
|
*/
|
||||||
export default function trialDivision(number) {
|
export default function trialDivision(number) {
|
||||||
if (number <= 0) {
|
if (number <= 1) {
|
||||||
// If number is less then 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;
|
||||||
} else if (number <= 3) {
|
} else if (number <= 3) {
|
||||||
// All numbers from 1 to 3 are prime.
|
// All numbers from 2 to 3 are prime.
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user