-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEulerProblem0023.java
More file actions
44 lines (40 loc) · 1.28 KB
/
EulerProblem0023.java
File metadata and controls
44 lines (40 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
import java.util.ArrayList;
public static void main(String[] args) {
ArrayList<Integer> abundants = findAbundantNumbers();
int[] summable = new int[28123];
long total = 0;
for (int i = 0; i < 28123; i++) {
summable[i] = -1;
}
for (int i = 0; i < abundants.size(); i++) {
for (int j = 0; j < abundants.size(); j++) {
int sum = abundants.get(i)+abundants.get(j);
if (sum <= 28123) {
if (summable[sum-1] == -1) {
summable[sum-1] = 1;
}
}
}
}
for (int i = 0; i < 28123; i++) {
if (summable[i] == -1) {
total += i + 1;
}
}
System.out.println(total);
}
public static ArrayList<Integer> findAbundantNumbers() {
ArrayList<Integer> abundants = new ArrayList<>();
for (int i = 1; i <= 28123; i++) {
int sum = 0;
for (int j = 1; j <= Math.pow(i, 0.5); j++) {
if (i%j == 0) {
if ((j == 1) || (j == Math.pow(i, 0.5))) sum += j;
else sum += j + (i/j);
}
}
if ((sum > i) && (sum <= 28123)) abundants.add(i);
}
return abundants;
}