Build a Prime Number Sum Calculator - Build a Prime Number Sum Calculator

Tell us what’s happening:

I seem to be passing all the test cases for this lab except one which is the following:

6. sumPrimes(977) should return 73156.

Your code so far

// A Prime Number is a number only divisible by itself AND 1

function sumPrimes(number) {

  let sum = 0;

  let arr = [];




  // Generate list of numbers leading up to the given number (inclusive)

  for (let i = 2; i <= number; i++) {

    arr.push(i);

  }

  console.log("Initial Array:");

  console.log(arr);

  console.log("\n");




  // Remove every 2nd number in the list after 2 || Use .splice() to remove at specific index

  for (let i = 2; i < arr.length; i += 2) {

    arr[i] = 0;

  }

  // Mark with 0 THEN use .splice() to remove the 0's

  for (let i = 0; i < arr.length; i++) {

    if ((arr[i] === 0) === true) {

      arr.splice(i, 1);

    }

  }

  console.log("Removed multiples of 2:");

  console.log(arr);

  console.log("\n");




  // Remove every 3rd number in the list after 3 || Same method

  for (let i = 4; i < arr.length; i += 3) {

    arr[i] = 0;

  }

  for (let i = 0; i < arr.length; i++) {

    if ((arr[i] === 0) === true) {

      arr.splice(i, 1);

    }

  }

  console.log("Removed multiples of 3:");

  console.log(arr);

  console.log("\n");




  // Remove every 5th number in the list after 5 || Same method

  for (let i = 9; i < arr.length; i += 5) {

    arr[i] = 0;

  }

  for (let i = 0; i < arr.length; i++) {

    if ((arr[i] === 0) === true) {

      arr.splice(i, 1);

    }

  }

  console.log("Removed multiples of 5:");

  console.log(arr);

  console.log("\n");




  // Remove every 7th number in the list after 7 || Same method || Start at 49 (index 15)

  for (let i = 15; i < arr.length; i += 5) {

    arr[i] = 0;

  }

  for (let i = 0; i < arr.length; i++) {

    if ((arr[i] === 0) === true) {

      arr.splice(i, 1);

    }

  }

  console.log("Removed multiples of 7:");

  console.log(arr);

  console.log("\n"); 




  // Remove every 11th number in the list after 11 || Same method || Start at 77 (index 20)

  for (let i = 20; i < arr.length; i += 11) {

    arr[i] = 0;

  }

  for (let i = 0; i < arr.length; i++) {

    if ((arr[i] === 0) === true) {

      arr.splice(i, 1);

    }

  }

  console.log("Removed multiples of 11:");

  console.log(arr);

  console.log("\n");




  // Remove every 13th number in the list after 13 || Same method || Start at 65 (index 18)

  for (let i = 18; i < arr.length; i += 13) {

    arr[i] = 0;

  }

  for (let i = 0; i < arr.length; i++) {

    if ((arr[i] === 0) === true) {

      arr.splice(i, 1);

    }

  }

  console.log("Removed multiples of 13:");

  console.log(arr);

  console.log("\n"); 




  // Remove every 17th number in the list after 17 || Same method || Start at 119 (index 27)

  for (let i = 27; i < arr.length; i += 17) {

    arr[i] = 0;

  }

  for (let i = 0; i < arr.length; i++) {

    if ((arr[i] === 0) === true) {

      arr.splice(i, 1);

    }

  }

  console.log("Removed multiples of 17:");

  console.log(arr);

  console.log("\n"); 




  // Remove every 19th number in the list after 19 || Same method || Start at 95 (index 22) 

  for (let i = 22; i < arr.length; i += 19) {

    arr[i] = 0;

  }

  for (let i = 0; i < arr.length; i++) {

    if ((arr[i] === 0) === true) {

      arr.splice(i, 1);

    }

  }

  console.log("Removed multiples of 19:");

  console.log(arr);

  console.log("\n");




  console.log("\nIndex of 253: " + arr.indexOf(253));

  console.log("\n");




  // Remove every 23rd number in the list after 23 || Same method || Start at 253 (index 47)

  for (let i = 47; i < arr.length; i += 23) {

    arr[i] = 0;

  }

  for (let i = 0; i < arr.length; i++) {

    if ((arr[i] === 0) === true) {

      arr.splice(i, 1);

    }

  }

  console.log("Removed multiples of 23:");

  console.log(arr);

  console.log("\n");




  // Calculate the sum of prime numbers

  for (const element of arr) {

    sum += element;

  }




  return sum;

}




console.log(sumPrimes(977));

Instead of re-inventing the wheel with making my own algorithm, I just searched up ways to find prime numbers less than or equal to n then converted it into a functional program.

I referenced a Wikipedia article that covers the Sieve of Eratosthenes.

I can provide the link to the article if necessary.

What my program shows is what was covered in the article. However, this is more of a manual method as I have to remove numbers from the array repeatedly rather than just once.

Things went well for the following test cases:

Passed: 1. You should have a sumPrimes function.

Passed: 2. sumPrimes(10) should return 17.

Passed: 3. sumPrimes(5) should return 10.

Passed: 4. sumPrimes(2) should return 2.

Passed: 5. sumPrimes(0) should return 0.

However, for such a larger number things became inaccurate. My sum was slightly off.

My Sum: 72702

Expected Sum: 73156

I am wondering where I had went wrong in this program given I am replicating an existing algorithm and it seemed to work for most of the test cases except the last one.

Challenge Information:

Build a Prime Number Sum Calculator - Build a Prime Number Sum Calculator

It looks like you have hard-coded conditionals or variables that check for specific expected values. That is not solving this problem in the general case. Imagine if you were given different input values. Would your code be able to solve those problems?

To find out more about what hard-coding is or about why it is not suitable for solving coding questions, please read this post: Hard-coding For Beginners

Let us know if you have a question about how to make your code more flexible.

1 Like

Ah, I understand. Yeah, I was aware I was hard coding but definitely should avoid that entirely to develop problem solving skills. I went ahead with starting over and eventually made a more dynamic program that passed. Below is only a snippet of the code which represents the direction I took:

// A Prime Number is a number only divisible by itself AND 1

function sumPrimes(number) {

  let sum = 0;

  let arr = [];

  let masterArr = [];

  let tempVal;




  // Generate list of numbers leading up to the given number (inclusive)

  for (let i = 2; i <= number; i++) {

    arr.push(i);

  }

  console.log("Initial Array:");

  console.log(arr);

  console.log("\n");




  // Mark the elements

  for (let i = 0; i < arr.length; i++) {

    tempVal = arr[i];

    // console.log("Current tempVal: " + tempVal);




    for (let j = 0; j < arr.length; j++) {




      if (j !== tempVal && j !== 1) {

        if (tempVal % j === 0) {

          arr[i] = 0;

        }

      }

    }

  }




  console.log("Array with marked elements:");

  console.log(arr);

  console.log("\n");

Based off of this snippet, I am wondering if this is as flexible as it can be or if there are any possible improvements that may make it more efficient. I ask mainly for leetcode problems or technical questions in interviews.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.