wiki-greatest-common-divisor.rs

931 B · rust · 32 lines

1use mrlycore::errors::Result;2use mrlyfig::{ink, save, Board, Grid, Ramp};3use mrlynum::factor::gcd;4use mrlynum::lattice::coprime_pairs;56const SIDE: usize = 24;78fn main() -> Result<()> {9    let mut board = Board::square();10    let area = board.frame(0.08);11    let grid = Grid::new(area, SIDE, SIDE, 0.10);12    let ramp = Ramp::tone(ink::dim(), ink::blue());13    let mut lit = 0u64;14    for row in 0..SIDE {15        for col in 0..SIDE {16            let a = col + 1;17            let b = SIDE - row;18            let share = gcd(a, b);19            let tone = if share == 1 {20                lit += 1;21                ink::yellow()22            } else {23                ramp.at((share - 2) as f64 / (SIDE - 2) as f64)24            };25            grid.fill(&mut board, col, row, tone);26        }27    }28    assert_eq!(lit, coprime_pairs(SIDE));29    assert_eq!(lit, 359);30    save("wiki-greatest-common-divisor", &board)?;31    Ok(())32}