-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution23.java
More file actions
53 lines (49 loc) · 1.47 KB
/
Copy pathSolution23.java
File metadata and controls
53 lines (49 loc) · 1.47 KB
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.*;
public class Solution23{
static int divisorSum(int num){
int sqrt = (int)Math.sqrt(num);
int sum = 1;
//If number is perfect square, count square root only once.
if(sqrt * sqrt == num){
sum += sqrt;
sqrt--;
}
for(int i = 2; i <= sqrt; i++){
if (num % i == 0){
sum = sum + i + (num / i);
}
}
return sum;
}
static boolean isAbundant(int n){
return divisorSum(n) > n;
}
public static void main(String[] args) {
List<Integer> abundantNums = new ArrayList<Integer>();
for(int i = 12; i <= 20161; i++){
if(isAbundant(i)){
abundantNums.add(i);
}
}
int sum = 0;
//Check sums of abundant numbers
boolean[] abundantSums = new boolean[20162];
for(int j = 0; j < abundantNums.size();j++){
for(int k = j; k < abundantNums.size(); k++){
if(abundantNums.get(j) + abundantNums.get(k) <= 20161){
abundantSums[abundantNums.get(j) + abundantNums.get(k)] = true;
}
else{
break;
}
}
}
//Take sum of all numbers that cannot be expressed as the sum of two abundant numbers.
for(int i = 1; i <= 20161; i++){
if(!abundantSums[i]){
sum += i;
}
}
System.out.println(sum);
}
}