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