Find the sum of multiples of 3 and 5 below 1000

Ok guys, I'm making calls to Project Euler and can't believe I'm stuck on the first call . I really don't understand why I am getting the wrong answer, despite the fact that my code looks functional:

import java.util.ArrayList;


public class Multithree {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ArrayList<Integer> x = new ArrayList<Integer>();
        ArrayList<Integer> y = new ArrayList<Integer>();
        int totalforthree = 0;
        int totalforfive = 0;

        int total =0;

        for(int temp =0; temp < 1000 ; temp++){
            if(temp % 3 == 0){
                x.add(temp);
                totalforthree += temp;
            }
        }

        for(int temp =0; temp < 1000 ; temp++){
            if(temp % 5 == 0){
                y.add(temp);
                totalforfive += temp;
            }
        }

        total = totalforfive + totalforthree;



        System.out.println("The multiples of 3 or 5 up to 1000 are: " +total);

    }

}

I get the answer as 266333 and he says this is wrong ...

+5
source share
11 answers

you should use the same for loop for both double count aviod numbers that are multiples of it. 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;
        }
    }
+13
source

If you are using Java 8, you can do it like this:

Integer sum = IntStream.range(1, 1000) // create range
                  .filter(i -> i % 3 == 0 || i % 5 == 0) // filter out
                  .sum(); // output: 233168

, 3, 5 , , .map() 2 * i:

Integer sum = IntStream.range(1, 1000)
                  .filter(i -> i % 3 == 0 || i % 5 == 0)
                  .map(i -> i % 3 == 0 && i % 5 == 0 ? 2 * i : i)
                  .sum(); // output: 266333
+3


3 5.
.


; 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, .

= 33165.
: 266333
233168.
-
266333-33165 = 233168.

( )

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);
    }
}
+1

, n , n .

, . . 10 10 . .

:
:

  • 3 .
  • 5 .
  • 3 * 5 = 15 .

java CodeForWin - Project Euler 1: Multiples 3 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

, , ( ) i, 3 5 .

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;
}
0

. , Java:

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;
}

- , . .

0

. , , for if-else, , 3, 5.

 if(temp % 3 == 0){
     x.add(temp);
     totalforthree += temp;
 } else if(temp % 5 == 0){
     y.add(temp);
     totalforfive += temp;
 }
-1

, , , 3 5. - , 15, 30, 45, 60... 3, 5. , .

    public static void main(String[] args) {
    int Sum=0, i=0, j=0;
    for(i=0;i<=1000;i++)
        if (i%3==0 && i<=999)
            Sum=Sum+i;
    for(j=0;j<=1000;j++)
        if (j%5==0 && j<1000 && j*5%3!=0)
            Sum=Sum+j;
    System.out.println("The Sum is "+Sum);
}
-1

, , .

public class Multiples {

public static void main(String[]args) {
    int firstNumber = 3;
    int secondNumber = 5;
    ArrayList<Integer> numberToCheck = new ArrayList<Integer>();
    ArrayList<Integer> multiples = new ArrayList<Integer>();
    int sumOfMultiples = 0;
    for (int i = 0; i < 1000; i++) {
       numberToCheck.add(i);

       if (numberToCheck.get(i) % firstNumber == 0 || numberToCheck.get(i) % secondNumber == 0) {
           multiples.add(numberToCheck.get(i));
       }

    }

    for (int i=0; i<multiples.size(); i++) {

     sumOfMultiples += multiples.get(i);

    }
    System.out.println(multiples);
    System.out.println("Sum Of Multiples: " + sumOfMultiples);

}

}
-1

10, 3 - 3,6,9, 5 - 5,10, - 33, :

package com.parag;

/*
 * @author Parag Satav
 */
public class MultipleAddition {

    /**
     * @param args
     */
    public static void main( final String[] args ) {
    // TODO Auto-generated method stub

    ArrayList<Integer> x = new ArrayList<Integer>();
    ArrayList<Integer> y = new ArrayList<Integer>();
    int totalforthree = 0;
    int totalforfive = 0;
    int number = 8;

    int total = 0;

    for ( int temp = 1; temp <= number; temp++ ) {
        if ( temp % 3 == 0 ) {
            x.add( temp );
            totalforthree += temp;
        }

        else if ( temp % 5 == 0 ) {
            y.add( temp );
            totalforfive += temp;
        }
    }

    total = totalforfive + totalforthree;
    System.out.println( "multiples of 3 : " + x );
    System.out.println( "multiples of 5 : " + y );
    System.out.println( "The multiples of 3 or 5 up to " + number + " are: " + total );

}

}
-1
public class Solution {
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t>0){
            int sum = 0;
            int count =0;
            int n = sc.nextInt();
            n--; 
            System.out.println((n/3*(6+(n/3-1)*3))/2 + (n/5*(10+(n/5-1)*5))/2 - (n/15*(30+(n/15-1)*15))/2);
            t--;
    }
}
}
-1

All Articles