Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Sum of prime numbers between a range - JavaScript
We are required to write a JavaScript function that takes in two numbers, say a and b and returns the sum of all the prime numbers that fall between a and b. We should include a and b if they are prime as well.
Understanding Prime Numbers
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, etc.
Algorithm Overview
To solve this problem, we need two functions:
-
isPrime()- checks if a number is prime -
sumPrimesBetween()- finds all primes in range and calculates their sum
Method 1: Basic Implementation
const isPrime = (n) => {
if (n <= 1) return false;
if (n === 2) return true;
if (n % 2 === 0) return false;
for (let i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i === 0) {
return false;
}
}
return true;
};
const sumPrimesBetween = (start, end) => {
let sum = 0;
for (let i = start; i <= end; i++) {
if (isPrime(i)) {
sum += i;
}
}
return sum;
};
// Example usage
const num1 = 10;
const num2 = 30;
console.log(`Sum of primes between ${num1} and ${num2}:`, sumPrimesBetween(num1, num2));
console.log("Prime numbers in range:", getPrimesInRange(num1, num2));
function getPrimesInRange(start, end) {
const primes = [];
for (let i = start; i <= end; i++) {
if (isPrime(i)) {
primes.push(i);
}
}
return primes;
}
Sum of primes between 10 and 30: 129 Prime numbers in range: [ 11, 13, 17, 19, 23, 29 ]
Method 2: Optimized Version
For better performance with larger ranges, we can optimize the prime checking function:
const isPrimeOptimized = (n) => {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 === 0 || n % 3 === 0) return false;
for (let i = 5; i * i <= n; i += 6) {
if (n % i === 0 || n % (i + 2) === 0) {
return false;
}
}
return true;
};
const sumPrimesOptimized = (start, end) => {
let sum = 0;
let count = 0;
for (let i = start; i <= end; i++) {
if (isPrimeOptimized(i)) {
sum += i;
count++;
}
}
return { sum, count };
};
// Test with larger range
const result = sumPrimesOptimized(1, 100);
console.log(`Sum of first 25 primes (1-100): ${result.sum}`);
console.log(`Count of primes found: ${result.count}`);
Sum of first 25 primes (1-100): 1060 Count of primes found: 25
Performance Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| Basic Implementation | O(n?n) | Small ranges |
| Optimized Version | O(n?n) but faster | Large ranges |
Key Points
- Check divisibility only up to ?n for efficiency
- Skip even numbers after checking for 2
- Handle edge cases: numbers ? 1 are not prime
- Use 6k±1 optimization for better performance
Conclusion
Finding the sum of prime numbers in a range requires an efficient prime checking algorithm. The optimized version significantly improves performance for larger ranges while maintaining accuracy.
Advertisements
