wiki-prime-counting-function.rs

1.5 kB · rust · 48 lines

1use mrlycore::errors::Result;2use mrlyfig::{ink, plot, save, Board};3use mrlynum::classics::primes;4use mrlynum::formulas;56const TOP: usize = 200;7const SAMPLES: usize = 1200;89fn main() -> Result<()> {10    let mut board = Board::square();11    let frame = board.frame(0.08);12    let left = 2.0;13    let right = (TOP + 1) as f64;14    let count = formulas::prime_count(TOP);15    let peak = count as f64;16    let across = |x: f64| frame.x + frame.w * (x - left) / (right - left);17    let up = |v: f64| frame.y + frame.h * (1.0 - v / peak);1819    let roll = primes(TOP);20    let hair = ink::fade(ink::dim(), 0.30);21    for &p in &roll {22        let x = across(p as f64);23        board.segment((x, frame.y), (x, frame.y + frame.h), 1.4, hair);24    }2526    let mut guess = Vec::with_capacity(SAMPLES + 1);27    for k in 0..=SAMPLES {28        let x = left + (right - left) * k as f64 / SAMPLES as f64;29        guess.push((across(x), up(x / x.ln())));30    }31    board.polyline(&guess, 3.4, ink::blue());3233    let mut stair = Vec::with_capacity(2 * (TOP - 1));34    let mut seen = 0usize;35    for n in 2..=TOP {36        seen += roll.binary_search(&n).is_ok() as usize;37        stair.push((across(n as f64), up(seen as f64)));38        stair.push((across((n + 1) as f64), up(seen as f64)));39    }40    board.polyline(&stair, 3.4, ink::yellow());4142    plot::axis(&mut board, frame, ink::line());43    assert_eq!(count, 46);44    assert_eq!(roll.len(), count);45    assert_eq!(seen, count);46    save("wiki-prime-counting-function", &board)?;47    Ok(())48}