简体   繁体   中英

Why isn't my javascript returning the correct answer to Project Euler's Number One?

I'm attempting to solve Project Euls Problem Number One using Javascript. I know this isn't the most eloquent solution however I do not understand why it does not work. I am taking the multiples of three, five under 1000 then storing them in two seperate arrays. I'm then adding the arrays together, outputting the answer using console.log() and the answer I'm getting is 266333 instead of the correct answer 233168. Does anyone know why?

/* Declaring Global Variables */
var n; 
var sumOfThree = 0;
var sumOfFive = 0;

/* Declaring Arrays */
multiplesOfThree = [];
multiplesOfFive = [];

/* Finding how many numbers < 1000 divide evenly by three and five then adding them to my arrays*/

console.log("Let's calculate how many numbers divide evenly by three and five in the number one thousand.");
console.log("Calculating...");

for(n = 0; n < 1000; n ++) {
    if(n % 3 === 0) { 
    multiplesOfThree.push(n);
    }
}

for(n = 0; n < 1000; n ++) {
    if(n % 5 === 0) { 
        multiplesOfFive.push(n);
    }
}

/* Letting the User know how many multiples of three exist */

console.log()
console.log("There are " + multiplesOfThree.length + " multiples of three in the number one thousand.");

/* Letting the user know how many multiples of five exist */

console.log()
console.log("There are " + multiplesOfFive.length + " multiples of five in the number one thousand.");
console.log()

/*Letting the User know the sum of the number of multiples*/

console.log("Let's get the sum of the number of multiples.");
console.log("Calculating...");
console.log(multiplesOfThree.length + multiplesOfFive.length);
console.log()

/* Letting the user know the sum of all the three multiples*/
console.log("Let's get the sum of all the three multiples")
console.log("Calculating... ");
for (i=0; i < multiplesOfThree.length; i++) {

    sumOfThree += multiplesOfThree[i];

}
console.log(sumOfThree);
console.log()

/* Letting the User know the sum of all the five multiples */

console.log("Let's get the sum of five multiples")
console.log("Calculating... ");
for (i=0; i < multiplesOfFive.length; i++) {

    sumOfFive += multiplesOfFive[i];

}
console.log(sumOfFive);
console.log()

/* Letting the user know the added sum of all the three, five multiples */

console.log("Let's add these two sums together")
console.log("Calculating... ");
var sumOfBoth = sumOfFive + sumOfThree;
console.log(sumOfBoth);

是的,因为您要将3和5的倍数相加两次。

The sum of the numbers from 1 to N is N*(N+1)/2 .

To compute the sum you're trying to compute, we need to think about the parts. The sum of the numbers from 1 to 999 that are divisible by 3 is going to be the sum of the numbers from 1 to 333, times 3. Similarly, the sum of the numbers from 1 to 999 that are divisible by 5 will be the sum of the numbers from 1 to 199, times 5. Finally, we have to take into account that both of those sums will include numbers that are divisible by both 3 and 5, so we need to subtract out the sum of the numbers from 1 to 66, times 15.

Thus, the overall sum is:

3*(333*334)/2 + 5*(199*200)/2 - 15*(66*67)/2

The fact that the sum of the numbers from 1 to N is N*(N+1)/2 is I think generally attributed to Gauss, though it's not a very complicated relation so it may be much older. You can demonstrate that it's true by considering the values 1 through N written out, and the values N down to 1 written out immediately below. The sum of each top-and-bottom pair from both rows of values is clearly N+1 ; the first pair is N and 1, the second is N-1 and 2, the third is N-2 and 3, and so on. There are N of those pairs, so the result is clear: N instances of N+1 , and since the list is doubled we need to divide by 2 to get the final sum. Note that dividing by 2 cannot introduce a fraction, since either N or N+1 must be an even number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM