find the sum of the multiples of 3 and 5 below 1000

前端 未结 12 962
我在风中等你
我在风中等你 2021-01-18 08:04

Ok guys, so I\'m doing the Project Euler challenges and I can\'t believe I\'m stuck on the first challenge. I really can\'t see why I\'m getting the wrong answer despite my

相关标签:
12条回答
  • 2021-01-18 08:36

    you should use the same for loop for both to aviod double counting numbers that are multiple of both. such as 15,30...

       for(int temp =0; temp < 1000 ; temp++){
            if(temp % 3 == 0){
                x.add(temp);
                totalforthree += temp;
            }else if(temp % 5 == 0){
                y.add(temp);
                totalforfive += temp;
            }
        }
    
    0 讨论(0)
  • 2021-01-18 08:37
        int count = 0;
    for (int i = 1; i <= 1000 / 3; i++)
    {
    count = count + (i * 3);
    if (i < 1000 / 5 && !(i % 3 == 0))
    {
    count = count + (i * 5);
    }
    }
    
    0 讨论(0)
  • 2021-01-18 08:38

    Don't you all think instead of using loops to compute the sum of multiples, we can easily compute sum of n terms using a simple formula of Arithmetic Progression to compute sum of n terms.

    I evaluated results on both using loop and formula. Loops works simply valuable to short data ranges. But when the data ranges grows more than 1010 program takes more than hours to process the result with loops. But the same evaluates the result in milliseconds when using a simple formula of Arithmetic Progression.

    What we really need to do is:
    Algorithm :

    1. Compute the sum of multiples of 3 and add to sum.
    2. Compute the sum of multiples of 5 and add to sum.
    3. Compute the sum of multiples of 3*5 = 15 and subtract from sum.

    Here is code snippet in java from my blog post CodeForWin - Project Euler 1: Multiples of 3 and 5

    n--; //Since we need to compute the sum less than n.
    //Check if n is more than or equal to 3 then compute sum of all divisible by
    //3 and add to sum.  
    if(n>=3) {  
        totalElements = n/3;  
        sum += (totalElements * ( 3 + totalElements*3)) / 2;  
    }  
    
    //Check if n is more than or equal to 5 then compute sum of all elements   
    //divisible by 5 and add to sum.  
    if(n >= 5) {  
        totalElements = n/5;  
        sum += (totalElements * (5 + totalElements * 5)) / 2;  
    }  
    
    //Check if n is more than or equal to 15 then compute sum of all elements  
    //divisible by 15 and subtract from sum.  
    if(n >= 15) {  
        totalElements = n/15;  
        sum -= (totalElements * (15 + totalElements * 15)) / 2;  
    }  
    
    System.out.println(sum); 
    
    0 讨论(0)
  • 2021-01-18 08:43

    In a mathematical perspective,
    You did not consider about common factors between 3 and 5.
    Because there is double counting.


    ex; number 15 , 30 , 45 , 60 , 75 , 90 , 105 , 120 , 135 , 150 , 165 , 180 , 195 , 210 , 225 , 240 , 255 , 270 , 285 , 300 , 315 , 330 , 345 , 360 , 375 , 390 , 405 , 420 , 435 , 450 , 465 , 480 , 495 , 510 , 525 , 540 , 555 , 570 , 585 , 600 , 615 , 630 , 645 , 660 , 675 , 690 , 705 , 720 , 735 , 750 , 765 , 780 , 795 , 810 , 825 , 840 , 855 , 870 , 885 , 900 , 915 , 930 , 945 , 960 , 975 , 990 , are common factors.

    total of common factors = 33165.
    Your answer is 266333
    Correct answer is 233168.
    Your Answer - Total of common factors
    266333-33165=233168.

    (this is a code for getting common factors and Total of common factors )

    public static void main(String[] args) {
    
    System.out.println("The sum of the Common Factors : " + getCommonFactorSum());
    
    }
    
    private static int getCommonFactorSum() {
    int sum = 0;
    for (int i = 1; i < 1000; i++) {
        if (i % 3 == 0 && i % 5 == 0) {
            sum += i;
            System.out.println(i);
        }
    }
    
    0 讨论(0)
  • 2021-01-18 08:46

    I did this several ways and several times. The fastest, cleanest and simplest way to complete the required code for Java is this:

    public class MultiplesOf3And5 {
    
    public static void main(String[] args){
    
    System.out.println("The sum of the multiples of 3 and 5 is: " + getSum());
    
    }
    
    private static int getSum() {
    int sum = 0;
    for (int i = 1; i < 1000; i++) {
        if (i % 3 == 0 || i % 5 == 0) {
            sum += i;
        }
    }
    return sum;
    }
    

    If anyone has a suggestion to get it down to fewer lines of code, please let me know your solution. I'm new to programming.

    0 讨论(0)
  • 2021-01-18 08:47

    You are counting some numbers twice. What you have to do is add inside one for loop, and use an if-else statement where if you find multiples of 3, you do not count them in 5 as well.

     if(temp % 3 == 0){
         x.add(temp);
         totalforthree += temp;
     } else if(temp % 5 == 0){
         y.add(temp);
         totalforfive += temp;
     }
    
    0 讨论(0)
提交回复
热议问题