Project Euler #1 in Java

前端 未结 8 1646
旧时难觅i
旧时难觅i 2021-02-04 18:22

I\'m having problems with this code. I don\'t want to look at others, so I\'m wondering what\'s wrong with mine.

If we list all the natural numbers below 10 that are mu

8条回答
  •  时光取名叫无心
    2021-02-04 18:51

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

提交回复
热议问题