You are viewing a single comment's thread. Return to all 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; } }
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #1: Multiples of 3 and 5
You are viewing a single comment's thread. Return to all 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; } }