Prime Number JavaScript – Find and Print Prime Numbers in JS

Prime Number JavaScript : In this JavaScript tutorial, we will learn How to Find prime numbers using JavaScript. We will also see the How does Logic works behind the code to Find and print prime numbers in JavaScript. So, Let’s get started –

How do you check if a number is prime in Javascript?


Things, you should know –

To better understand this Prime number JavaScript tutorial, you should have at least fundamental understanding of these JavaScript Concepts before going further –

  • Variable declarations & definitions
  • Loops in JavaScript (for…in / for Loop)
  • Functions, Classes or Methods
  • And, Some useful Operators (like + Addition operator,  * Multiplication operator).

Finding Prime Numbers in Javascript

To find Prime Number in JavaScript, you need to know what are Prime numbers? In our Number system, Prime

In simple words, Prime numbers are special natural numbers that are greater than 1 and are only divided by 1 and themselves.

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers
Src: Wikipedia

For example, 13 is a Prime number because it can only be divided by only 1 and 13.

However, 14 is not a Prime number because it can be divided by more than two numbers, and those are : 1, 2, 7 and 14 itself.

Now, we know what are the prime numbers. Let’s understand, How to Find prime numbers using JavaScript –


1) Solution to Find Prime numbers in JavaScript

Here is the code snippet of Finding and printing Prime Numbers –

function isPrimeNumber( num ) {                               // Explaining : Step-by-Step

        for (let k = 2; k <= Math.sqrt(num); k++) {          // Step 1: Check till Square root
                if( num%k == 0 )                                               //  Step 2: Check whether Remainder is ZERO or not?
                        return false;                                              //  Step 3: If, any other number divided our num., return 'false'
        }
        return true;                                                               // Else, return 'true', to indicate - It's a Prime Number
}

function findPrimeNumbers() {                                   // Define : A Driver Function

        console.log( 'Is 33 a Prime Number : ' + isPrimeNumber(33) );      // Calling our method - isPrimeNumber(xyz)
        console.log( 'Is 43 a Prime Number : ' + isPrimeNumber(43) );
        console.log( 'Is 53 a Prime Number : ' + isPrimeNumber(53) );
        console.log( 'Is 73 a Prime Number : ' + isPrimeNumber(73) );
        console.log( 'Is 63 a Prime Number : ' + isPrimeNumber(63) );
}

 

Execute the Code :

You can run the above JavaScript Prime Number program in any JS code editor such as Google console. To open the console press Control + Shift + I (in windows) or Command + Shift + I (in macOS). The developer tool box would be opened.

Now click on Console tab > Paste the code and Hit enter to Run it like this –

Find prime number using Functions in JavaScript

 

Output :

As you can verify in above Screen-grab, we have successfully got this output that tells, numbers are Prime or Not –

Is 33 a Prime Number : false 
Is 43 a Prime Number : true 
Is 53 a Prime Number : true 
Is 73 a Prime Number : true 
Is 63 a Prime Number : false

Logic : Behind the Code

In the above JavaScript program,

  • First, we have defined isPrimeNumber(…) function.
  • It takes a number to check its primality.
  • Inside this function, we ran a loop from k = 2 to k < squareRootOfNumber
  • Remember, a given number would only be a Prime number, if there exists NO OTHER NUMBER (except 1 and itself), that divides the given number.
  • In above program, we tested for number 33. Let’s see, How 33 is NOT a Prime number –
  • If you would calculate, then you’ll find that the Square Root of 33 is 5.74
    As, there EXIST a number 3, which is less than the Sq. Root 5.74, and divides 33 in 11 times. That’s why, 33 is NOT a Prime number and we got false.
  • But, When we tested for 43, there exists NO SUCH NUMBER less than the Sq. Root of 43 i.e. 6.557 , which could divide it completely. That’s why, 43 is a Prime number and we got true.
  • This is how the above JavaScript Prime number program works.

Wrap Up 

So far, we have learned How to Find prime numbers using JavaScript. I’ve also tried to explain the working logic behind the code.

If you have any doubt, then do comments or send me an email via Google, I’ll try my best to give you the solution within a day.

If you found this tutorial helpful, Please rate it on Facebook and Don’t forget to check Other Useful JavaScript Articles that you may like –

Leave a Comment