Need help to calculate the sum of numbers by omitting somes of them

Viewed 155

#1. I need to write a Java program that calculates the sum of numbers from 1 through 10,000 (including 1 and 10,000) but omitting numbers that are divisible by three and numbers whose hundred digit is 2 or 3 (for example 8200 or 5312).

I begin with but didnt work:

public class Sum10000 {

    public static void main(String[] arg) {
        long i = 0;
        long sum = 0;

        while (i != 10000) {
            sum = sum + i;
            i++;

            if ((i % 3) == 0 || (i >= 200 && i <= 399) || (i >= 1200 && i <= 1399)
                    || (i >= 2200 && i <= 2399) || (i >= 3200 && i <= 3399) || (i >= 4200 && i <= 4399)
                    || (i >= 5200 && i <= 5399) || (i >= 6200 && i <= 6399) || (i >= 7200 && i <= 7399)
                    || (i >= 8200 && i <= 8399) || (i >= 9200 && i <= 9399)) {
                continue;
            }
            System.out.println( i);
            System.out.println(sum);

        }
    }
}
6 Answers

You should only increment the sum after testing the condition.

while (i != 10000) {
    i++;
    if ((i % 3) == 0 || (i >= 200 && i <= 399) || (i >= 1200 && i <= 1399) ||
        (i >= 2200 && i <= 2399) || (i >= 3200 && i <= 3399) || (i >= 4200 && i <= 4399) ||
        (i >= 5200 && i <= 5399) || (i >= 6200 && i <= 6399) || (i >= 7200 && i <= 7399) ||
        (i >= 8200 && i <= 8399) || (i >= 9200 && i <= 9399)) {
        continue;
    }
    sum += i;
    System.out.println(i);
    System.out.println(sum);
}

Using a Stream can simplify this:

System.out.println(IntStream.rangeClosed(1, 10000)
  .filter(x -> x % 3 != 0 && x / 100 % 10 != 2 && x / 100 % 10 != 3).sum());

You are not skipping the numbers you intend to.

Firstly your loop needs to run including your last number so you should be checking for i<=10000

Secondly, when you have a number you want to skip you need to actually skip it, you're just skipping the printing of it! instead try somthing like this:

public class Sum10000 {

    public static void main(String[] arg) {
        long i = 1;
        long sum = 0;

        while (i != 10000) {
            while ((i % 3) == 0 || (i >= 200 && i <= 399) || (i >= 1200 && i <= 1399)
                    || (i >= 2200 && i <= 2399) || (i >= 3200 && i <= 3399) || (i >= 4200 && i <= 4399)
                    || (i >= 5200 && i <= 5399) || (i >= 6200 && i <= 6399) || (i >= 7200 && i <= 7399)
                    || (i >= 8200 && i <= 8399) || (i >= 9200 && i <= 9399)) {
                i++;
            }

            sum = sum + i;
            System.out.println( i);
            System.out.println(sum);
        }
    }
}

My understanding of the question is omitting numbers from the sum: that's why the sum is placed after the continue directive.

i % 1000 can be used in order to obtain the last three digits of i:

for (int sum=0, i=1; i<= 10000; ++i) {
    int test = i % 1000;
    
    if ((test >= 200 && test < 400) || i % 3 == 0)
        continue;

    sum += i;
    System.out.println("i   = " + i);
    System.out.println("sum = " + sum);
}

Why not test your conditions programmatically ?

int sum = 0;
for (int i = 1; i <= 10000; i++) {
    if (i % 3 == 0) {
        System.out.println("Exclude " + i + ": multiple of " + 3);
        continue;
    }
    String decimal = "" + i;
    if (decimal.length() > 2) {
        char dig3 = decimal.charAt(decimal.length() - 3);
        if (dig3 == '2' || dig3 == '3') {
            System.out.println("Exclude " + i + ": 100s digit is " + dig3);
            continue;
        }
    }
    sum += i;
}
System.out.println(sum);
System.out.println("Or formatted: " + NumberFormat.getIntegerInstance().format(sum));

public class Sum10000 {

public static void main(String[] arg) {
    
    long sum = 0;

    for (int i = 1, 1!=10001, i++) {

        if (!((i % 3) == 0 || (i >= 200 && i <= 399) || (i >= 1200 && i <= 1399)
                || (i >= 2200 && i <= 2399) || (i >= 3200 && i <= 3399) || (i >= 4200 && i <= 4399)
                || (i >= 5200 && i <= 5399) || (i >= 6200 && i <= 6399) || (i >= 7200 && i <= 7399)
                || (i >= 8200 && i <= 8399) || (i >= 9200 && i <= 9399))) {

            sum = sum + i;

        }
    }
        
    System.out.println(sum);
}

}

To add one more alternative using regex, which IMO is more readable than dealing with 3 or more condition in the while or if block:

public static void main(String[] args) {
    int sum = 0;
    for(int i = 1; i <=10_000; i++){
        String s = String.valueOf(i);
        if( i%3 != 0 && !s.matches(".*[23]..") ){
            sum += i;
        }
    }
    System.out.println(sum);
}  
Related