-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendATable.java
47 lines (42 loc) · 1.01 KB
/
SendATable.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
/** #number-theory #math */
import java.util.ArrayList;
import java.util.Scanner;
class SendATable {
static int max = 50000;
public static ArrayList<Long> getPreCalculatedNum() {
ArrayList<Long> result = new ArrayList<>();
result.add(0L); // 0
result.add(1L); // 1
long phiVal;
for (int i = 2; i <= max; i++) {
phiVal = 2 * phi(i);
result.add(result.get(i - 1) + phiVal);
}
return result;
}
public static int phi(int n) {
int result = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0) {
n /= i;
}
result = result / i * (i - 1);
}
}
if (n > 1) {
result = result / n * (n - 1);
}
return result;
}
public static void main(String[] args) {
ArrayList<Long> calculatedArr = getPreCalculatedNum();
Scanner sc = new Scanner(System.in);
int n;
while (true) {
n = sc.nextInt();
if (n == 0) break;
System.out.println(calculatedArr.get(n));
}
}
}