
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Primes in Ranges using JavaScript
Prime numbers are numbers that have exactly two perfect divisors. We will see two methods to find the number of prime numbers in a given range. The first is using the brute force method and by this method time complexity is a bit high. Then we will improve this method and will go for the Sieve of the Eratosthenes algorithm to go with better time complexity. In this article, we are going to find the total number of prime numbers in the given range using the JavaScript programming language.
Brute Force Method
In this method first, we will learn how to find a number that is a prime number or not we can find that in two methods. One method is with the O(N) time complexity and another method is with O(sqrt(N)) time complexity.
The Direct Method to find a Number is a Prime Number or not
Example
First, we will go through the for a loop until the number and count the number that can perfectly divide the number, if the number that can divide the number is not equal to 2 then the number is not a prime number otherwise the number is a prime number. Let's see the code ?
function isPrime(number){ var count = 0; for(var i = 1;i<=number;i++) { if(number%i == 0){ count = count + 1; } } if(count == 2){ return true; } else{ return false; } } // checking if 13 and 14 are prime numbers or not if(isPrime(13)){ console.log("13 is the Prime number"); } else{ console.log("13 is not a Prime number") } if(isPrime(14)){ console.log("14 is the Prime number"); } else{ console.log("14 is not a Prime number") }
In the above code, we have traversed from 1 to number which is the range in which we can find the numbers that can divide the given number and got the count that how many numbers can divide the given number, and printed the result based on that.
The time complexity of the above code is O(N) and to check every number if they are prime or not will take O(N*N) which means this is not a good method to check.
Mathematical Approach
We know when a number divides another number perfectly then the quotient is also a perfect integer that is if a number p is perfectly divisible by a number q and the quotient is r means q * r = p. Also r can also divide the number p perfectly with the quotient q. So, this implies a prefect divisor always come in pair.
Example
By the above discussion, we can conclude that if we just go for checking the number division only up to the square root of N, then it will give the same result in very less time. Let's see the code of the above method ?
function isPrime(number){ if(number == 1) return false var count = 0; for(var i = 1;i*i<=number;i++){ if(number%i == 0){ count = count + 2; } } if(count == 2){ return true; } else{ return false; } } // checking if 67 and 99 are prime numbers or not if(isPrime(67)){ console.log("67 is the Prime number"); } else{ console.log("67 is not a Prime number") } if(isPrime(99)){ console.log("99 is the Prime number"); } else{ console.log("99 is not a Prime number") }
In the above code, we have just changed the previous code by changing the range of the for loop as now it will just check up to only the first square root of N elements and we increased the count by 2.
The time complexity of the above code is O(sqrt(N)) which is better, which means we can use this method to find the number of the prime number present in the given range.
Number of Prime Numbers in Range L to R
Example
We will just implement the previously given code in the range and count the number of Prime Numbers in the given range. Let's implement the code ?
function isPrime(number){ if(number == 1) return false var count = 0; for(var i = 1;i*i<=number;i++) { if(number%i == 0){ count = count + 2; } } if(count == 2){ return true; } else{ return false; } } var L = 10 var R = 5000 var count = 0 for(var i = L; i <= R; i++){ if(isPrime(i)){ count = count + 1; } } console.log(" The number of Prime Numbers in the given Range is: " + count);
In the above code, we have traversed in the range from L to R using a for loop and at each iteration, we have checked whether the current number is a Prime number or not. If the number is the prime number then we have increased the count and at last, printed that value.
The time complexity of the above code is O(N*N) where N is the number of elements in the Range.
Sieve of the Eratosthenes Algorithm
Example
Sieve of Eratosthenes algorithm works in an efficient way and finds the number of prime numbers in a given range in O(Nlog(log(N))) time which is very fast as compared to the other algorithms. The Space taken by the sieve is O(N) but it won't matter as time is very efficient. Let's see the code, then we will move to the explanation of the code ?
var L = 10 var R = 5000 var arr = Array.apply(null, Array(R+1)).map(Number.prototype.valueOf,1); arr[0] = 0 arr[1] = 0 for(var i = 2;i<=R;i++){ if(arr[i] == 0){ continue; } for(var j = 2; i*j <= R; j++){ arr[i*j] = 0; } } var pre = Array.apply(null, Array(R+1)).map(Number.prototype.valueOf,0); for(var i = 1; i<= R;i++){ pre[i] = pre[i-1] + arr[i]; } answer = pre[R]-pre[L-1] console.log("The number of Prime Numbers in the given Range is: " + answer);
In the above code, we have seen the implementation of the sieve of Eratosthenes. First, we created an array containing ones of size R, after that we have traversed over the array using for loop, and for each iteration, if the current number is not one means it is not prime else it is prime and we have removed all the number which are less than R and multiple of a current prime number. Then we created a prefix array that will store the count of the prime number from 0 to the current index and answers to each query in the range 0 to R can be provided in Constant time.
Time and Space Complexity
The time complexity of the above-given code is O(N*log(log(N))) which is far better as compared to both O(N*N) and O(N*(sqrt(N))). The space complexity of the above code is more as compared to previous codes and is O(N).
Conclusion
In this tutorial, we have seen how to find the number of prime numbers in the given range using the JavaScript programming language. Prime numbers are numbers that have exactly two perfect divisors. One is not a prime number because it has only one perfect divisor. We have seen three methods with the time complexity of O(N*N), O(N*sqrt(N)), and O(N*log(log(N))). Also, the space complexity is O(1) for the first two methods, and for the sieve of Eratosthenes space complexity is O(N).