Problem 52 "Permuted multiples"

It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

問 52 「置換倍数」

125874を2倍すると251748となる. これは元の数125874と順番は違うが同じ数を含む.

2x, 3x, 4x, 5x, 6x が x と同じ数を含むような最小の正整数 x を求めよ.

const BLANK: [u8; 10] = [0u8; 10];

fn histogram(mut n: u32, digits: &mut [u8; 10]) {
    digits.copy_from_slice(&BLANK);
    while n > 0 {
        let d = (n % 10) as usize;
        digits[d] += 1;
        n /= 10;
    }
}

fn explorarion(place: u32, digit_matrix: &mut [[u8; 10]; 6]) -> Option<u32> {
    'next_x: for x in place..place * 10 / 6 {
        histogram(x, &mut digit_matrix[0]);
        for a in 2usize..=6 {
            histogram(a as u32 * x, &mut digit_matrix[a - 1]);
            if digit_matrix[0] != digit_matrix[a - 1] {
                continue 'next_x;
            }
        }
        return Some(x);
    }
    None
}

fn main() {
    let mut digit_matrix = [[0u8; 10]; 6];
    let mut place = 100u32;
    let n = loop {
        match explorarion(place, &mut digit_matrix) {
            Some(n) => break n,
            None => place *= 10,
        }
    };
    println!("{}", n);
    assert_eq!(n, 142857);
}