-
Notifications
You must be signed in to change notification settings - Fork 0
/
P23NonabundantSums.java
47 lines (41 loc) · 1018 Bytes
/
P23NonabundantSums.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
import java.util.HashMap;
import java.util.Iterator;
public class P23NonabundantSums {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer, Integer> abundantMap = new HashMap<Integer, Integer>();
int max = 28123;
int min = 12;
for (int i=min; i<= max; i++)
if (IsAbundant(i))
abundantMap.put(i, 1);
long sum = 0;
for (int i=1; i <= max; i++)
{
//if (abundantMap.containsKey(i)) continue;
Iterator<Integer> keySetIterator = abundantMap.keySet().iterator();
Boolean contFlag = false;
while(keySetIterator.hasNext()){
Integer key = keySetIterator.next();
Integer val = i-key;
if (abundantMap.containsKey(val))
{
contFlag = true;
break;
}
}
if (contFlag) continue;
sum = sum + i;
}
System.out.println("" + sum);
}
public static Boolean IsAbundant(int n)
{
int sum = 0;
for (int i=1;i<n;i++)
if (n%i==0)
sum = sum + i;
if (sum > n) return true;
else return false;
}
}