-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq21.java
More file actions
27 lines (25 loc) · 693 Bytes
/
q21.java
File metadata and controls
27 lines (25 loc) · 693 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
import java.util.*;
public class q21 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hashtable<Integer, Integer> amicableNumbers = new Hashtable<Integer,Integer>();
int result = 0;
for(int i = 0; i < 10000; ++i){
int sumOfDivs = sumOfDivisors(i);
if(amicableNumbers.containsKey(sumOfDivs) && amicableNumbers.get(sumOfDivs) == i){
result += i + sumOfDivs;
}else if(!amicableNumbers.containsKey(sumOfDivs)){
amicableNumbers.put(i, sumOfDivs);
}
}
System.out.println(result);
}
public static int sumOfDivisors(int x){
int result = 0;
for(int i = 1; i < x ; ++i ){
if(x % i == 0)
result += i;
}
return result;
}
}