1
\$\begingroup\$

I have written a code to check if the number is prime number are not.

I pass the number from command line argument, and check if the number is prime.

I have written a function for it which returns the boolean value.

(function(){
    'use strict';

    main();

    function main(){
        var testNum = getCommandLineVariables();
        console.log("Is Number \""+testNum+"\" a prime number ? ",isPrimeNumber(testNum));
    }


    function isPrimeNumber(num){
        var halfNum = num/2;
        halfNum = parseInt(halfNum);

        // check if number is divisible by 2, 
        // if yes then its not a prime number.
        if(num%2 === 0){
            return false;
        }


        do{
            // if number is divisible by its half value, 
            // if yes, than its not prime number    
            if(num%halfNum === 0){
                return false;
            }

        }while(--halfNum >= 2); // reduce the half-value by 1 and continue

        return true;
    } // end of isPrimeNumber

    // get the number from commandline argument
    function getCommandLineVariables(){
        var arg_array = process.argv.slice(2);

        if(arg_array.length !== 1){
            throw new Error("Pass a number");
        }


        return parseInt(arg_array[0]);
    } // end of getCommandLineVariables 
})();

I run the program as follows,

E:\math>node isPrimeNum01.js 13
Is Number "13" a prime number ?  true

E:\math>node isPrimeNum01.js 16
Is Number "16" a prime number ?  false

E:\math>node isPrimeNum01.js 29
Is Number "29" a prime number ?  true

E:\math>node isPrimeNum01.js 113
Is Number "113" a prime number ?  true

E:\math>node isPrimeNum01.js 127
Is Number "127" a prime number ?  true

E:\math>node isPrimeNum01.js 566
Is Number "566" a prime number ?  false

E:\math>

Please review my code and leave your valuable comments. Also please check whether my logic for checking the prime number is efficient.

\$\endgroup\$
  • \$\begingroup\$ instead of just half the number, you can take its square root \$\endgroup\$ – juvian Feb 5 '18 at 16:06
2
\$\begingroup\$

I will just be looking at your prime function.

You got the right idea, but you missed some edge cases, and it can also be optimized. 2 and 3 are prime but return false, since halfNum starts at 1, and do...while loops always run at least once. 1 is not prime but return true, along with some negative numbers. The easiest way to handle this is just to check for this special case at the start of the function.

if(n <= 1) return false;

You are checking for divisibility with all numbers between 2 and half the input, which is good. But it seems a bit confusing to me when you use a do...while loop and counting down, instead of just counting up with a good old for loop.

for(let i = 2; i <= halfNum; i++)

But we can optimize this, since we only need to check up to the square root of the number. For any divisible number larger than the square root, there will also be a divisible number lower than the square root. For example, 100 is divisible by 20, but also 5, since 20*5=100. I'm also going to pre-calculate it and rename halfNum as that name doesn't fit anymore.

const limit = Math.sqrt(num);

The final function looks like this.

function isPrime(num) {
    if(num <= 1) return false;
    const limit = Math.floor(Math.sqrt(num));
    for(let i = 2; i <= limit; i++) {
        if(num % i === 0) return false;
    }
    return true;
}
\$\endgroup\$
  • \$\begingroup\$ kind of missing checking if num % 2 == 0 return false after checking special case 2. isPrime(4) returns true as of now \$\endgroup\$ – juvian Feb 5 '18 at 20:47
  • 1
    \$\begingroup\$ @juvian I was mixing it up with a function that check all numbers if they are prime, instead of just a single number. I should probably have tested this before posting, but I have fixed it now. \$\endgroup\$ – Kruga Feb 6 '18 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.