Open In App

Javascript Program for Prime Numbers

What are prime numbers?

Prime numbers

Some interesting facts about Prime numbers:

(p - 1) ! ≡  -1   mod p 
OR  (p - 1) ! ≡  (p-1) mod p
an-1 ≡ 1 (mod n)
OR 
an-1 % n = 1

Properties of prime numbers:

Prime numbers and co-prime numbers:

It is important to distinguish between prime numbers and co-prime numbers. Listed below are the differences between prime and co-prime numbers.

How do we check whether a number is Prime or not?

Naive Approach: A naive solution is to iterate through all numbers from 2 to sqrt(n) and for every number check if it divides n. If we find any number that divides, we return false.



Example: Below is the implementation of above approach:




<script>
    function isPrime(n) {
        // Corner case
        if (n <= 1)
            return false;
  
        // Check from 2 to n-1
        for (let i = 2; i < n; i++)
            if (n % i == 0)
                return false;
  
        return true;
    }
  
    // Driver Code
  
    isPrime(11) 
        ? console.log("true"
        : console.log("false");
</script>

Output:



true

Time Complexity: O(sqrt(n)).
Auxiliary Space: O(1).

Efficient approach: To check whether the number is prime or not follow the below idea:

In the previous approach given if the size of the given number is too large then its square root will be also very large, so to deal with large size input we will deal with a few numbers such as 1, 2, 3, and the numbers which are divisible by 2 and 3 in separate cases and for remaining numbers, we will iterate our loop from 5 to sqrt(n) and check for each iteration whether that  (iteration) or (that iteration + 2) divides n or not. If we find any number that divides, we return false.

Example: Below is the implementation of above approach:




<script>
    function isPrime(n) {
        // Check if n=1 or n=0
        if (n <= 1)
            return false;
        // Check if n=2 or n=3
        if (n == 2 || n == 3)
            return true;
        // Check whether n is divisible by 2 or 3
        if (n % 2 == 0 || n % 3 == 0)
            return false;
        // Check from 5 to square root of n
        // Iterate i by (i+6)
        for (var i = 5; i <= Math.sqrt(n); i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
  
        return true;
    }
  
    // Driver Code
    isPrime(11) 
        ? console.log("true"
        : console.log("false");
</script>

Output:

true

Time Complexity: O(sqrt(n)).
Auxiliary Space: O(1).

Approach 3: To check whether the number is prime or not using recursion follow the below idea:

Recursion can also be used to check if a number between 2 to n – 1 divides n. If we find any number that divides, we return false.

Example: Below is the implementation of above approach:




<script>
    function isPrime(n) {
        var i = 1;
  
        // corner cases
        if (n == 0 || n == 1) {
            return false;
        }
  
        // Checking Prime
        if (n == i) return true;
  
        // base cases
        if (n % i == 0) {
            return false;
        }
        i++;
        return isPrime(n);
    }
  
    // Driver Code
  
    isPrime(35) 
        ? console.log(" true\n"
        : console.log(" false\n");
</script>

Output:

false

Time Complexity: O(N).
Auxiliary Space: O(N).


Article Tags :