-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project Euler #21: Amicable numbers
85 lines (79 loc) · 2.05 KB
/
Project Euler #21: Amicable numbers
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.*;
import java.util.*;
public class Solution {
static long[][] result = null;
//1<=N<=100000
static int limit = 100000;
/*
* initialization
*/
static {
//stores the amicable numbers
Set<Integer> set = bar(limit);
int k = 0;
result = new long[set.size()][2];
for (Integer i : set) {
result[k][0] = i;
if (k > 0) {
result[k][1] = result[k - 1][1] + i;
} else {
result[k][1] = i;
}
k++;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
int N = in.nextInt();
getResult(N);
}
in.close();
}
private static int foo(int N) {
int sum = 0;
for (int i = 2; i * i < N; i++) {
if (N % i == 0)
sum += (i + N / i);
}
if (sum == 0)
return sum;
// add 1
sum++;
if (sum == N)
return 0;
return sum;
}
private static TreeSet<Integer> bar(int N) {
TreeSet<Integer> set = new TreeSet<>();
for (int i = 2; i <= N; i++) {
if (set.contains(i))
continue;
// if (i == 300) {
// System.out.println();
// }
int k = foo(i);// converted
int M = foo(k);
if (M == i) {
set.add(i);
set.add(k);
}
}
return set;
}
private static void getResult(int N) {
if (N <= result[0][0]) {
System.out.println(0);
} else if (N > result[result.length - 1][0]) {
System.out.println(result[result.length - 1][1]);
} else {
for (int i = 1; i < result.length; i++) {
if (N < result[i][0]) {
System.out.println(result[i - 1][1]);
break;
}
}
}
}
}