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
const T1: [u8; 10] = [50, 0, 1, 7, 2, 23, 8, 33, 3, 14];
const T2: [u8; 101] = [
    1, 2, 4, 8, 16, 32, 64, 3, 6, 12, 24, 48, 0, 0, 9, 18, 36, 72, 0, 0, 0, 27, 54, 5, 10, 20, 40,
    0, 81, 0, 15, 30, 0, 7, 14, 28, 56, 45, 0, 0, 21, 42, 0, 0, 0, 0, 25, 63, 0, 0, 0, 0, 0, 0, 0,
    0, 35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

/// Percy Ludgate's algorithm for multiplying two single-digit numbers,
/// optimized for a mechanical computer - it only uses addition and
/// one-dimensional table lookups.
///
/// # Panics
///
/// Panics if either argument is 10 or larger.
pub fn irish_log(a: u8, b: u8) -> u8 {
    T2[(T1[a as usize] + T1[b as usize]) as usize]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn valid_values() {
        for a in 0..9 {
            for b in 0..9 {
                assert_eq!(irish_log(a, b), a * b);
            }
        }
    }

    #[test]
    #[should_panic]
    fn invalid_a() {
        irish_log(10, 0);
    }

    #[test]
    #[should_panic]
    fn invalid_b() {
        irish_log(0, 10);
    }
}