wiki-ulam-spiral.rs

815 B · rust · 29 lines

1use mrlycore::errors::Result;2use mrlyfig::{ink, save, Board};3use mrlynum::spiral::{flags, Lattice};45const SIDE: usize = 101;6const REACH: i64 = 50;7const PRIMES: usize = 1252;89fn main() -> Result<()> {10    let mut board = Board::square();11    let frame = board.frame(0.06);12    let unit = frame.w / SIDE as f64;13    let top = SIDE * SIDE;14    let prime = flags(top);15    let mut lit = 0usize;16    for n in 1..=top as u64 {17        if !prime[n as usize] {18            continue;19        }20        let (x, y) = Lattice::Square.xy(n);21        let px = frame.x + (x + REACH) as f64 * unit;22        let py = frame.y + (REACH - y) as f64 * unit;23        board.rect(px, py, unit * 0.86, unit * 0.86, ink::blue());24        lit += 1;25    }26    assert_eq!(lit, PRIMES);27    save("wiki-ulam-spiral", &board)?;28    Ok(())29}