# Sum of all prime numbers in JavaScript

In the given problem statement we have to compute the sum of all the prime numbers with the help of Javascript functionalities. So for calculating the sum we will set the upper limit of primes numbers.

## Understanding the Problem

The problem is to calculate the sum of all the prime numbers up to a given limit. So the prime number is a positive integer which is greater than 1 and also these numbers do not have any divisors other than 1 and itself. For example we have a limit of the primes is 10, so the prime numbers less than 10 are 2, 3, 5, 7 and the sum of these numbers is 2 + 3 + 5 + 7 = 17. So the required result should be 17. So our task is to implement the code to produce the sum of the prime numbers with the limit given.

## Logic for the given Problem

For solving the problem we will use Javascript. And for this we will basically use two functions to get the desired result.

The first function will be created to check that the current number is prime or not. And we will also verify if there are any divisors between 2 and the square root of the number. If we have found the divisor for the current number then we can say that the number is not prime because prime numbers are not divisible by any other number. In the result we will say that the number is not prime. And if the criteria is not verified then we will return the number as a prime number.

The second function is used to compute the sum of all prime numbers inside a declared range. So we will utilize a loop to traverse the values. Then using the first function we have defined, we will determine the number is prime. If the number is prime then add the value to get the sum and finally we will obtain our required result as the sum of all prime numbers.

## Algorithm

Step 1: As we have to compute the sum of the prime number up to the given limit. So for this we will define a function and pass a parameter num as an input.

Step 2: As we know that the prime numbers start from 2. So we will define a condition to check that the given input number is greater than or equal to the 1. This condition checks if there are any negative numbers present because negative numbers are not prime numbers, so return as false.

Step 3: Now we will use a for loop to iterate the numbers from 2 to square root of the number. And check the condition if a divisor is found then the number is not a prime number so we will return false.

Step 4: Outside the above condition, if no divisors are found then the number is a prime number so we will return true.

Step 5: With the usage of another function we will compute the sum of all the prime numbers up to a given number. And we will pass a parameter as limit in this function. This limit input will define a range that the prime numbers will be added to.

Step 6: As we need to store the sum of the given prime numbers then we will use a variable to store the sum of the prime numbers and initialize the value to zero.

Step 7: Now we need to iterate the number starting from 2 to the limit. Inside the loop we will check that the number is a prime number. Add the sum into the sum variable if we encountered the prime number.

Step 8: As we need to show the sum variable to the console then we will return the value of sum.

### Example

 // Function to check the number is prime
function isPrime(num) {
if (num <= 1) {  // 1 and negative numbers are not prime
return false;
}
// check for divisors from 2 to square root of the number
for (let i = 2; i <= Math.sqrt(num); i++) {
// if a divisor is found, the number is not prime
if (num % i === 0) {
return false;
}
}
// if no divisors are found, the number is prime
return true;
}
// Function to get the sum of all the primes
function sumOfPrimes(limit) {
let sum = 0;

for (let i = 2; i <= limit; i++) {
// check if the number is prime
if (isPrime(i)) {
// add the prime number to the sum
sum += i;
}
}

return sum;  // return the final sum
}

const limit = 50; // Change this value to set the upper limit
const sum = sumOfPrimes(limit);
console.log("Sum of prime numbers up to", limit, "is", sum);


### Output

Sum of prime numbers up to 50 is 328


## Complexity

The first function for checking that the number is prime or not uses a loop to iterate the numbers so the complexity of this function is O(sqrt(n)), in which n is the given number. And space consumed by this function is O(1) because it uses a fixed amount of memory for variables.

The second function iterates the prime numbers from 2 to the given limit. For every iteration we have called the above function which has a time complexity of O(sqrt(n)). So the time complexity of this function is O(limit * sqrt(n)). And the space complexity of this function is also O(1), as the function is using a fixed amount of memory for variables.

## Conclusion

The code we have created provides an efficient way of calculating the sum of all the prime numbers up to a given limit in Javascript. As we have implemented two functions to get the required sum. The time complexity of this function depends on the limit and the largest number within the limit.

Updated on: 16-Aug-2023

2K+ Views

Get certified by completing the course