-
Notifications
You must be signed in to change notification settings - Fork 1
/
Problem023_NonAbundantSums.java
68 lines (48 loc) · 1.63 KB
/
Problem023_NonAbundantSums.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.ArrayList;
/**
* Created by Sneeuwpopsneeuw on 16-Aug-16.
*/
public class Problem023_NonAbundantSums {
public static void main(String args[]) {
long startTime = System.currentTimeMillis();
int ans = 0, value = 0;
ArrayList<Integer> map = new ArrayList<>();
for (int i=1; i <= 28123 ; i++) {
if ( d(i) > i) {
// System.out.println("abundant number i = "+i+" d(i) = "+d(i));
map.add(i);
}
}
System.out.println("abundant number map generated");
boolean flag = false;
int checkedValue = 0;
for (int i=1; i <= 28123 ; i++) {
for (int j=0; j < map.size() ; j++) {
checkedValue = map.get(j);
if (map.contains(i-checkedValue)) {
flag = true;
break;
} else if (checkedValue > i) break;
}
if (flag == false) {
System.out.println(" -- "+ans+" - i = " + i);
ans += i;
}
flag = false;
}
System.out.println("ans: "+ans);
System.out.println("Time: " + (System.currentTimeMillis() - startTime) +" ms");
}
private static int d(int value) {
int divisors = 1;
for (int j=2; j<value; j++) { // we could a loop that goes from 2 to the sqrt of the value
if (value % j == 0) {
divisors += j; // If we did ^^ we also need to calculate value / 2 and add that to divisors
}
}
return divisors;
}
}
/*
1 -> 28123
*/