JavaAlgorithms/ProjectEuler/Problem01.java

52 lines
1.1 KiB
Java
Raw Normal View History

2020-09-13 17:23:51 +08:00
package ProjectEuler;
/**
2020-10-24 18:23:28 +08:00
* If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
* The sum of these multiples is 23.
*
* <p>Find the sum of all the multiples of 3 or 5 below 1000.
*
* <p>Link: https://projecteuler.net/problem=1
2020-09-13 17:23:51 +08:00
*/
public class Problem01 {
2020-10-24 18:23:28 +08:00
public static void main(String[] args) {
int[][] testNumber = {
{3, 0},
{4, 3},
{10, 23},
{1000, 233168},
{-1, 0}
};
for (int[] ints : testNumber) {
assert solution1(ints[0]) == ints[1];
assert solution2(ints[0]) == ints[1];
2020-09-13 17:23:51 +08:00
}
2020-10-24 18:23:28 +08:00
}
private static int solution1(int n) {
int sum = 0;
for (int i = 3; i < n; ++i) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
2020-09-13 17:23:51 +08:00
}
2020-10-24 18:23:28 +08:00
return sum;
}
2020-09-13 17:23:51 +08:00
2020-10-24 18:23:28 +08:00
private static int solution2(int n) {
int sum = 0;
2020-09-13 17:23:51 +08:00
2020-10-24 18:23:28 +08:00
int terms = (n - 1) / 3;
sum += terms * (6 + (terms - 1) * 3) / 2;
2020-09-13 17:23:51 +08:00
2020-10-24 18:23:28 +08:00
terms = (n - 1) / 5;
sum += terms * (10 + (terms - 1) * 5) / 2;
2020-09-13 17:23:51 +08:00
2020-10-24 18:23:28 +08:00
terms = (n - 1) / 15;
sum -= terms * (30 + (terms - 1) * 15) / 2;
2020-09-13 17:23:51 +08:00
2020-10-24 18:23:28 +08:00
return sum;
}
}