-
Notifications
You must be signed in to change notification settings - Fork 0
/
e40.rs
49 lines (45 loc) · 948 Bytes
/
e40.rs
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
fn exp(b: int, e: int) -> int {
if e == 0 {
1
} else {
b * exp(b, e - 1)
}
}
fn getDigit(x: int, n: int, k: int) -> int {
if n == k - 1 {
x % 10
} else {
getDigit(x / 10, n, k - 1)
}
}
/**The minimum n such that d_n indexes into a k-digit number*/
fn min_n(k: int) -> int {
let mut i = 1;
let mut sum = 0;
while i < k {
sum += i * exp(10, i -1);
i += 1;
}
1 + (9 * sum)
}
fn d_n(n: int) -> int {
let mut min = 1;
let mut k = 1;
while min_n(k) <= n {
min = min_n(k);
k += 1
}
k -= 1;
let numIndex = (n - min) / k;
let digiIndex = (n - min) % k;
getDigit(exp(10, k - 1) + numIndex, digiIndex, k)
}
fn main() {
let mut prod = 1;
let mut n = 1;
while n <= 1000000 {
prod *= d_n(n);
n *= 10;
}
println(fmt!("%d", prod));
}