Project Euler #1: Multiples of 3 and 5

  • + 0 comments

    for(int a0 = 0; a0 < t; a0++){ int n = in.nextInt(); System.out.println(sumOfMultiples(n-1)); }}

    private static long sumOfMultiples(int n) { return sumDivisibleBy(n, 3) + sumDivisibleBy(n, 5) - sumDivisibleBy(n, 15); } private static long sumDivisibleBy(int n, int k) { int p = n / k; return k * (long)p * (p + 1) / 2; } }