mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
Don't treat 1 as prime number.
This commit is contained in:
parent
f0ddaf243c
commit
5503cced48
@ -4,7 +4,7 @@ import trialDivision from '../trialDivision';
|
||||
* @param {function(n: number)} testFunction
|
||||
*/
|
||||
function primalityTest(testFunction) {
|
||||
expect(testFunction(1)).toBeTruthy();
|
||||
expect(testFunction(1)).toBeFalsy();
|
||||
expect(testFunction(2)).toBeTruthy();
|
||||
expect(testFunction(3)).toBeTruthy();
|
||||
expect(testFunction(5)).toBeTruthy();
|
||||
|
@ -3,11 +3,11 @@
|
||||
* @return {boolean}
|
||||
*/
|
||||
export default function trialDivision(number) {
|
||||
if (number <= 0) {
|
||||
// If number is less then one then it isn't prime by definition.
|
||||
if (number <= 1) {
|
||||
// If number is less than one then it isn't prime by definition.
|
||||
return false;
|
||||
} else if (number <= 3) {
|
||||
// All numbers from 1 to 3 are prime.
|
||||
// All numbers from 2 to 3 are prime.
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user