Javascript Program for Prime Numbers
Last Updated : 28 Aug, 2024
What are prime numbers?
Prime numbers- In other words, the prime number is a positive integer greater than 1 that has exactly two factors, 1 and the number itself.
- There are many prime numbers, such as 2, 3, 5, 7, 11, 13, etc.
- Keep in mind that 1 cannot be either prime or composite.
- The remaining numbers, except for 1, are classified as prime and composite numbers.
Some interesting facts about Prime numbers:
- Except for 2, which is the smallest prime number and the only even prime number, all prime numbers are odd numbers.
- Every prime number can be represented in the form of 6n + 1 or 6n - 1 except the prime numbers 2 and 3, where n is a natural number.
- Two and Three are only two consecutive natural numbers that are prime.
- Goldbach Conjecture: Every even integer greater than 2 can be expressed as the sum of two primes.
- Wilson Theorem: Wilson’s theorem states that a natural number p > 1 is a prime number if and only if
(p - 1) ! ≡ -1 mod p
OR (p - 1) ! ≡ (p-1) mod p
an-1 ≡ 1 (mod n)
OR
an-1 % n = 1
- Prime Number Theorem: The probability that a given, randomly chosen number n is prime is inversely proportional to its number of digits, or to the logarithm of n.
- Lemoine’s Conjecture: Any odd integer greater than 5 can be expressed as a sum of an odd prime (all primes other than 2 are odd) and an even semiprime. A semiprime number is a product of two prime numbers. This is called Lemoine’s conjecture.
Properties of prime numbers:
- Every number greater than 1 can be divided by at least one prime number.
- Every even positive integer greater than 2 can be expressed as the sum of two primes.
- Except 2, all other prime numbers are odd. In other words, we can say that 2 is the only even prime number.
- Two prime numbers are always coprime to each other.
- Each composite number can be factored into prime factors and individually all of these are unique in nature.
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.
- A coprime number is always considered as a pair, whereas a prime number is considered as a single number.
- Co-prime numbers are numbers that have no common factor except 1. In contrast, prime numbers do not have such a condition.
- A co-prime number can be either prime or composite, but its greatest common factor (GCF) must always be 1. Unlike composite numbers, prime numbers have only two factors, 1 and the number itself.
- Example of co-prime: 13 and 15 are co-primes. The factors of 13 are 1 and 13 and the factors of 15 are 1, 3 and 5. We can see that they have only 1 as their common factor, therefore, they are coprime numbers.
- Example of prime: A few examples of prime numbers are 2, 3, 5, 7 and 11 etc.
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:
JavaScript 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");
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:
JavaScript 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");
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:
JavaScript 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");
Time Complexity: O(N).
Auxiliary Space: O(N).
Similar Reads
JavaScript Program to Print Prime Numbers from 1 to N A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this article, we'll explore how to create a JavaScript program to print all prime numbers from 1 to a given number N. To find prime numbers from 1 to N, we need to: Iterate through all numbers
3 min read
JavaScript Program to Check Prime Number By Creating a Function A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this article, we will explore how to check if a given number is a prime number in JavaScript by creating a function. Table of Content Check Prime Number using for LoopCheck Prime Number using
2 min read
Recursive program for prime number Given a number n, check whether it's prime number or not using recursion.Examples: Input : n = 11 Output : Yes Input : n = 15 Output : No The idea is based on school method to check for prime numbers. C++ // CPP Program to find whether a Number // is Prime or Not using Recursion #include <bits/st
4 min read
Javascript Program for Count Primes in Ranges Given a range [L, R], we need to find the count of total numbers of prime numbers in the range [L, R] where 0 <= L <= R < 10000. Consider that there are a large number of queries for different ranges.Examples: Input : Query 1 : L = 1, R = 10 Query 2 : L = 5, R = 10Output : 4 2Explanation :P
3 min read
Program to find the next prime number Given an integer N. The task is to find the next prime number i.e. the smallest prime number greater than N. Examples: Input: N = 10 Output: 11 11 is the smallest prime number greater than 10. Input: N = 0 Output: 2 Approach: First of all, take a boolean variable found and initialize it to false.Now
5 min read
Program to print prime numbers from 1 to N. Given a number N, the task is to print the prime numbers from 1 to N.Examples: Input: N = 10Output: 2, 3, 5, 7Explanation : The output "2, 3, 5, 7" for input N = 10 represents the list of the prime numbers less than or equal to 10. Input: N = 5Output: 2, 3, 5 Explanation : The output "2, 3, 5" for i
15+ min read