nimber 0.1.1

Library for calculating in Conway's nim-arithmetic.
Documentation
//! Basic demonstration of some nimber arithmetic operations, by
//! printing multiplication tables and similar.
//!
//! All these tables correspond to a sequence in OEIS (links in the
//! output). The addition and multiplication tables can also be
//! checked visually against [the ones on
//! Wikipedia](https://en.wikipedia.org/wiki/Nimber#Addition_and_multiplication_tables).

use nimber::FiniteNimber;

fn main() {
    println!(
        "Addition table for nimbers less than 16 (https://oeis.org/A003987):"
    );
    for y in (0..16).map(|y| FiniteNimber::from(y)) {
        for (xi, x) in (0..16).map(|x| FiniteNimber::from(x)).enumerate() {
            let product = x + &y;
            print!(
                "{}{:x}",
                if xi == 0 { "" } else { " " },
                product.as_slice()[0]
            );
        }
        println!();
    }

    println!();

    println!("Multiplication table for nimbers less than 16 (https://oeis.org/A051775):");
    for y in (0..16).map(|y| FiniteNimber::from(y)) {
        for (xi, x) in (0..16).map(|x| FiniteNimber::from(x)).enumerate() {
            let product = x * &y;
            print!(
                "{}{:x}",
                if xi == 0 { "" } else { " " },
                product.as_slice()[0]
            );
        }
        println!();
    }

    println!();

    println!("Multiplication table for powers of 2 less than 2^16 (https://oeis.org/A223541):");
    for y in (0..16).map(|y| FiniteNimber::from(1 << y)) {
        for (xi, x) in (0..16).map(|x| FiniteNimber::from(1 << x)).enumerate()
        {
            let product = x * &y;
            print!(
                "{}{:04x}",
                if xi == 0 { "" } else { " " },
                product.as_slice()[0]
            );
        }
        println!();
    }

    println!();

    println!("Multiplicative inverses of nimbers up to 2^8 (https://oeis.org/A051917):");
    for y in 0..16 {
        for (xi, x) in (0..16).enumerate() {
            let n = FiniteNimber::from(y * 16 + x);
            let invstr = match n.inverse() {
                None => "--".into(),
                Some(inv) => format!("{:02x}", inv.as_slice()[0]),
            };
            print!("{}{}", if xi == 0 { "" } else { " " }, invstr);
        }
        println!();
    }

    println!();

    println!("Square roots of nimbers up to 2^8 (https://oeis.org/A160679):");
    for y in 0..16 {
        for (xi, x) in (0..16).enumerate() {
            let n = FiniteNimber::from(y * 16 + x);
            let sqrt = n.sqrt();
            print!(
                "{}{:02x}",
                if xi == 0 { "" } else { " " },
                sqrt.as_slice()[0]
            );
        }
        println!();
    }
}