Euler Project # 1 in Java

I am having problems with this code. I do not want to look at others, so I wonder what happened to you.

If we list all natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6, and 9. The sum of these multiple values ​​is 23.

Find the sum of all multiples of 3 or 5 below 1000.

public class Multiples {
    public static void main (String [] args) {
        int temp = 0;
        int temp2 = 0; 

        for (int i = 0; i <= 1000; i++) {
            if (i % 3 == 0) {
                temp = temp + i;
            }            
        }

        for (int j = 0; j <= 1000; j++) {
            if (j % 5 == 0) {
                temp2 = temp2 + j;
            }
        }

        System.out.println(temp + temp2);
    }
}

The value I get is 267333, which is incorrect. I'm not wrong? I know algorithmically, this code may not match, but it should work, right?

+4
source share
7 answers

Decision

1) O (n):

A slight improvement for other answers ( imay start with 3):

public static void main(String[] args) {
    int sum = 0;
    for (int i = 3; i < 1000; i++) {
        if (i % 3 == 0 || i % 5 == 0) {
            sum += i;
        }
    }
    System.out.println(sum);
}

For a larger input number ( Integer.MAX_VALUEinstead 1000), you need:

  • 195 seconds

2) O (n) = O (n/3) + O (n/5) + O (n/15):

( , ):

public static void main(String[] args) {
    long sum = 0 ;
    for ( long i = 3 ; i < 1000 ; i+=3 ){
        sum+=i;
    }
    for ( long i = 5 ; i < 1000 ; i+=5 ){
        sum+=i;
    }       
    for ( long i = 15 ; i < 1000 ; i+=15 ){
        sum-=i;
    }
    System.out.println(sum);
}

n (1000) i, n/3 + n/5 + n/15 (600) i. - , ( if).

(Integer.MAX_VALUE 1000) :

  • 9

3) O (1):

:

1 + 2 +... + n = n * (n + 1)/2

public static void main(String[] args) {
    int nr = 1000;
    nr--;
    int x3 = nr/3;
    int x5 = nr/5;
    int x15 = nr/15;

    long sum1 = 3*x3*(x3+1); 
    long sum2 = 5*x5*(x5+1);
    long sum3 = 15*x15*(x15+1);

    long sum = (sum1+sum2-sum3)/2;
    System.out.println(sum);
}

, Integer.MAX_VALUE, ( 1 ).

+11

:

for (int i = 0; i < 1000; i++) {
    if (i % 3 == 0 || i % 5 ==0) {
        temp += i;
    }
}

. 15 , .

, < 1000, <=.

+7

3, 5 (, 15, 30, 45 ..), . for , :

public class Multiples {
    public static void main (String [] args) {
    int temp = 0;

    for (int i = 0; i < 1000; i++) {
        if (i % 3 == 0 || i % 5 == 0) {
            temp = temp + i;
        }

    }

    System.out.println (temp);
   }
}
+1

15 . , , 15, .

0

- : , 3 5. : , - . , :

. : , 3, 5 - .

. Python, , , . , .

numlist = []

for i in range (1, 1000):
    if i % 3 == 0:
        numlist.append(i)


for j in range (1, 1000):
    if j % 5 == 0:
        if not j in numlist:
            numlist.append(j)


counter = 0
for a in numlist:
    counter += a


print counter
0

Some conditions arise when both conditions satisfy both 3 and 5. For example, when I = 15 satisfies both 15% 3 == 0 and 15% 5 == 0. so your answer is probably more than expected because you tried both for 3 and 5 separately. By doing this during these specific conditions, you add duplicate values. Therefore, it is better to check one cycle. like this -

    for(int i=0 ; i<1000 ; i++)
    {
        if(i%3==0 || i%5==0)
        {
            temp = temp + i;
        }
        System.out.println(temp);
    }
0
source
public static int sumOfMultiples(int i, int j, int limit){
    int s = --limit / i, t = limit / j, u = limit / (i * j);
    return (i*(s*(s+1)/2)) + (j*(t*(t+1)/2)) - ((i*j)*(u*(u+1)/2));
}

Test

actual = Prob1.sumOfMultiples(3, 5, 1000);
assertEquals(233168, actual);
-1
source

Source: https://habr.com/ru/post/1524028/


All Articles