-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem-21.java
49 lines (39 loc) · 1.03 KB
/
problem-21.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
import java.util.Hashtable;
import java.util.HashSet;
import java.util.Iterator;
public class Problem21 {
public static void main(String[] args) {
Hashtable<Integer, Integer> divisorTable = new Hashtable<Integer, Integer>(10000);
HashSet<Integer> numTable = new HashSet<Integer>(10000);
int divisorSum;
for (int i = 1; i <= 10000; i++) {
divisorSum = getDivSum(i);
divisorTable.put(i, divisorSum);
}
int currSum;
int amiSum = 0;
for (int i = 1; i <= 10000; i++) {
currSum = divisorTable.get(i);
for (int j = i+1; j <= 10000; j++) {
if (currSum == j && divisorTable.get(j) == i) {
numTable.add(i);
numTable.add(j);
}
}
}
Iterator<Integer> numIterator = numTable.iterator();
while (numIterator.hasNext()) {
amiSum += numIterator.next();
}
System.out.println("Sum of the amicable numbers under 10000 is: " + amiSum);
}
public static int getDivSum(int a) {
int divSum = 1;
for (int i = 2; i*i <= a; i++) {
if (a % i == 0) {
divSum += i + (a/i);
}
}
return divSum;
}
}