-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProblem_023.java
More file actions
46 lines (34 loc) · 911 Bytes
/
Problem_023.java
File metadata and controls
46 lines (34 loc) · 911 Bytes
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
45
46
package euler;
import java.util.HashSet;
public class Problem_023 {
static int nonAbundantSum() {
return nonAbundantSum(28123);
}
private static int nonAbundantSum(int upperbound) {
HashSet<Integer> abundantNums = new HashSet<Integer>(); // data structures are important (w/ arraylist rt = 40 seconds)
int sum = 0;
boolean abundantSum = false;
for(int i = 1; i <= upperbound; i++) {
if(isAbundant(i)) abundantNums.add(i);
for(Integer num : abundantNums) {
if(abundantNums.contains(i - num)) {
// System.out.println(num + " + " + (i - num) + " = " + i);
abundantSum = true;
break;
}
}
if(abundantSum) abundantSum = false;
else sum += i;
}
return sum;
}
private static boolean isAbundant(int n) {
int sum = 0;
for(int i = 1; i < n; i++) {
if(n / i == (double) n / i) {
sum += i;
}
}
return sum > n;
}
}