-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem23.java
57 lines (42 loc) · 1 KB
/
Problem23.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
//Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
// Make code more memory efficient!
package com.prog.ProjectEulerPrograms;
public class Problem23 {
public static void main(String[] args) {
int i = 1, j = 1, sum = 0;
boolean isAbundant = false;
for(i = 1; i<=20161 ; i++) {
for(j = 1; j<i && isAbundant == false; j++) {
if(isAbundant(j)) {
if(isAbundant(i-j)) {
isAbundant = true;
}
}
}
if(isAbundant == false)
sum += i;
isAbundant = false;
}
System.out.println(sum+" is the answer!");
}
private static boolean isAbundant(int num) {
int i, sum = 0;
boolean result = false;
for(i = 1; i<=Math.sqrt(num); i++) {
if(num%i == 0) {
// if divisors are equal,
// take only one of them
if(num / i == i)
sum += i;
else {
sum += i;
sum = sum + (num/i);
}
sum = sum - num;
}
}
if(sum > num)
result = true;
return result;
}
}