Problem 40 "Champernowne's constant"

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021...

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fractional part, find the value of the following expression.

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

問 40 「チャンパーノウン定数」

正の整数を順に連結して得られる以下の10進の無理数を考える:

0.123456789101112131415161718192021...

小数第12位は1である.

dnで小数第n位の数を表す. \( d_{1} × d_{10} × d_{100} × d_{1000} × d_{10000} × d_{100000} × d_{1000000} \) を求めよ.

struct Container {
    capacity: u32,
    elements: u32,
}

fn digit_at(nth: u32) -> u8 {
    let mut container = Container {
        capacity: 0,
        elements: 0,
    };
    let mut w = 1u32;
    loop {
        let elements = 10u32.pow(w) - 10u32.pow(w - 1);
        let capacity = w * elements;
        if nth < container.capacity + capacity {
            break;
        }
        container.capacity += capacity;
        container.elements += elements;
        w += 1;
    }
    let residue = nth - container.capacity;
    if residue % w == 0 {
        return ((container.elements + residue / w) % 10) as u8;
    }
    let num = container.elements + residue / w + 1;
    ((num / 10u32.pow(w - residue % w)) % 10) as u8
}

fn main() {
    let p = (0u32..=6)
        .map(|d| 10u32.pow(d))
        .map(|d| digit_at(d))
        .map(|d| d as u32)
        .product::<u32>();

    println!("{}", p);
    assert_eq!(p, 210);

    assert_eq!(digit_at(1), 1);
    assert_eq!(digit_at(9), 9);
    assert_eq!(digit_at(17), 3);
    assert_eq!(digit_at(18), 1);
    assert_eq!(digit_at(189), 9);
    assert_eq!(digit_at(190), 1);
    assert_eq!(digit_at(194), 0);
    assert_eq!(digit_at(37371), 6);
}