cogputer/
lib.rs

1const T1: [u8; 10] = [50, 0, 1, 7, 2, 23, 8, 33, 3, 14];
2const T2: [u8; 101] = [
3    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,
4    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,
5    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,
6    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7];
8
9/// Percy Ludgate's algorithm for multiplying two single-digit numbers,
10/// optimized for a mechanical computer - it only uses addition and
11/// one-dimensional table lookups.
12///
13/// # Panics
14///
15/// Panics if either argument is 10 or larger.
16pub fn irish_log(a: u8, b: u8) -> u8 {
17    T2[(T1[a as usize] + T1[b as usize]) as usize]
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23
24    #[test]
25    fn valid_values() {
26        for a in 0..9 {
27            for b in 0..9 {
28                assert_eq!(irish_log(a, b), a * b);
29            }
30        }
31    }
32
33    #[test]
34    #[should_panic]
35    fn invalid_a() {
36        irish_log(10, 0);
37    }
38
39    #[test]
40    #[should_panic]
41    fn invalid_b() {
42        irish_log(0, 10);
43    }
44}