KokoBear
KokoBear

Reputation: 101

Get the sum of first N primes javascript

I am trying to find the sum of the first N primes, below I have attached my code. I am struggling with the last part (getting the sum). So far I have defined what a prime number is and got the first N prime numbers

function isPrime(number) {
  if (number <= 1) return false;
  if (number === 2) return true;
  else {
    for (let i = 2; i < number; i++) {
      if (number % i === 0) return false;
    }
    return true;
  }
}
console.log(isPrime(6)); //false 

function getNprimes(n) {
  const arr = [];
  let i = 2

  while (arr.length < n) {
    if (isPrime(i)) {
      arr.push(i)
    }
    i++
  }
  return arr;
}
console.log(getNprimes(5)); //[2, 3, 5, 7, 11]

const sumOfNPrimes = (num) => {
  let sum = getNprimes(num);
  if (sum === 0) {
    sum = getNprimes(num + 1)
    return sum;
  }
}
console.log(sumOfNPrimes(4));

Upvotes: 1

Views: 964

Answers (2)

Spectric
Spectric

Reputation: 31992

The check sum === 0 will always return false since sum is an array and you are using strict equality, which checks for types. You should be checking the length property instead, and using the != operator (e.g, only execute the code if the length of the array is not 0).

To calculate the sum of the resulting array, you can use Array#reduce:

function isPrime(number) {
  if (number <= 1) return false;
  if (number === 2) return true;
  else {
    for (let i = 2; i < number; i++) {
      if (number % i === 0) return false;
    }
    return true;
  }
}
console.log(isPrime(6)); //false 

function getNprimes(n) {
  const arr = [];
  let i = 2

  while (arr.length < n) {
    if (isPrime(i)) {
      arr.push(i)
    }
    i++
  }
  return arr;
}
console.log(getNprimes(5)); //[2, 3, 5, 7, 11]

const sumOfNPrimes = (num) => {
  let sum = getNprimes(num).reduce((a, b) => a + b);
  return sum
}
console.log(sumOfNPrimes(4));

Upvotes: 3

navpahul551
navpahul551

Reputation: 13

The algorithm you have implemented will run in O(N sqrt(N)) and it is finding the sum for every query. So, if you want to reduce the time complexity of your code then you can implement sieve of erastosthenes which runs in O(N log (log N)) and store the values of sums of prime numbers in O(N) and then answer all queries in O(1) time complexity. Below is the implementation of this idea in javascript:

let primes = [];
let primeSum = [2];
const N = 1000001;

function generatePrimes(){
    let isPrime = [];
    for(let i=1; i<N; i++) isPrime.push(true);
    for(let i=2; i<N; i++){
        if(!isPrime[i]) continue; // if number is not prime
        primes.push(i); // add prime number
        for(let j=2*i; j<N; j += i){
            isPrime[j] = false; // marking all multiples of                  current prime as non prime
        }
    }

// calculate prime sums from 0 index to ith index
    for(let i=1; i<primes.length; i++){
        if(i < 10) console.log(primes[i]);
        primeSum.push(Number(Number(primes[i])+Number(primeSum[i-    1])));
    }
}

generatePrimes();

// answer query in O(1) for n = 10:
const n = 3;
console.log(primeSum[n-1]);

you can also answer l to r range query prime sums with this code

ps: sorry for bad english

Upvotes: 1

Related Questions