-
Notifications
You must be signed in to change notification settings - Fork 1
/
Euler023.java
53 lines (47 loc) · 1.38 KB
/
Euler023.java
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
47
48
49
50
51
52
53
import java.util.HashSet;
public class Euler023
{
public static final int MAX = 28123;
public static void main (String[] args)
{
long start = System.currentTimeMillis();
int[] abundant = new int[MAX];
int acount = 0;
for (int ii = 1; ii <= MAX; ii++) {
if (ii < sumdiv(ii)) {
abundant[acount++] = ii;
}
}
HashSet<Integer> integers = new HashSet<Integer>();
for (int ii = 0; ii < MAX; ii++) {
integers.add(ii+1);
}
for (int aa = 0; aa < acount; aa++) {
for (int bb = aa; bb < acount; bb++) {
integers.remove(abundant[aa] + abundant[bb]);
}
}
int total = 0;
for (Integer value : integers) {
total += value;
}
System.out.println(total);
System.out.println("[total " + (System.currentTimeMillis() - start) + "ms]");
}
protected static int sumdiv (int value)
{
int sum = 1;
for (int ii = 2, ll = (int)Math.ceil(Math.sqrt(value)); ii < ll; ii++) {
if (value % ii == 0) {
sum += ii;
sum += (value / ii);
}
}
double root = Math.sqrt(value);
int iroot = (int)root;
if (root > 1 && root == iroot) {
sum += iroot;
}
return sum;
}
}