-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path070-totient-permutation.js
More file actions
70 lines (56 loc) · 1.48 KB
/
070-totient-permutation.js
File metadata and controls
70 lines (56 loc) · 1.48 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Totient Permutation
* Time Complexity: O(N log log N)
* Space Complexity: O(N)
*/
function processData(input) {
const N = parseInt(input.trim(), 10);
const phi = new Array(N + 1);
for (let i = 0; i <= N; i++) phi[i] = i;
for (let p = 2; p <= N; p++) {
if (phi[p] === p) {
for (let k = p; k <= N; k += p) {
phi[k] -= phi[k] / p;
}
}
}
function isPermutation(a, b) {
const ca = new Array(10).fill(0);
const cb = new Array(10).fill(0);
while (a > 0) {
ca[a % 10]++;
a = Math.floor(a / 10);
}
while (b > 0) {
cb[b % 10]++;
b = Math.floor(b / 10);
}
for (let i = 0; i < 10; i++) {
if (ca[i] !== cb[i]) return false;
}
return true;
}
let bestN = 0;
let bestRatio = Number.POSITIVE_INFINITY;
for (let n = 2; n < N; n++) {
const ph = phi[n];
if (!isPermutation(n, ph)) continue;
const ratio = n / ph;
if (ratio < bestRatio - 1e-15) {
bestRatio = ratio;
bestN = n;
} else if (Math.abs(ratio - bestRatio) < 1e-15 && n < bestN) {
bestN = n;
}
}
console.log(bestN);
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});