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

前端 未结 12 963
我在风中等你
我在风中等你 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:50
    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--;
        }
    }
    }
    
    0 讨论(0)
  • 2021-01-18 08:51

    If number is 10 then multiple of 3 is 3,6,9 and multiple of 5 is 5,10 total sum is 33 and program gives same answer:

    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 );
    
    }
    
    }
    
    0 讨论(0)
  • 2021-01-18 08:54

    How I solved this is that I took an integer value (initialized to zero) and kept on adding the incremented value of i, if its modulo with 3 or 5 gives me zero.

    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 讨论(0)
  • 2021-01-18 08:55

    Logics given above are showing wrong answer, because multiples of 3 & 5 are taken for calculation. There is something being missed in above logic, i.e., 15, 30, 45, 60... are the multiple of 3 and also multiple of 5. then we need to ignore such while adding.

        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);
    }
    
    0 讨论(0)
  • 2021-01-18 08:56

    Okay, so this isn't the best looking code, but it get's the job done.

    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);
    
    }
    
    }
    
    0 讨论(0)
  • 2021-01-18 09:01

    If you are using Java 8 you can do it in the following way:

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

    To count the numbers which are divisible by both 3 and 5 twice you can either write the above line twice or .map() the 2 * i values:

    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
    
    0 讨论(0)
提交回复
热议问题