34: Improve solution for Rust track exercise "Nth Prime".

[?]
Aaw9nJhsNmfzFih9mKyNw9mV8CgERXJkRa1kK1Kx3LQH
Sep 3, 2021, 12:04 PM
YGJVWTKAANDS6TUXWRI5V4TLVWTZNBNX2HXRYJ2WSPBPI2U6MB2AC

Dependencies

  • [2] XDEY7SNL 31: Improve solution for Rust track exercise "Raindrops".
  • [3] VH5OFL55 32: Improve solution for Rust track exercise "Nth Prime".
  • [4] JZN2AQ3E 10: Add Rust track exercise "nth-prime".
  • [5] ERYD4CD7 11: Add solution for Rust track exercise "Nth Prime".
  • [6] IXMBS6ZP 29: Improve solution for Rust track exercise "Nth Prime".
  • [7] UTP7D53Q 12: Improve solution for Rust track exercise "Nth Prime".
  • [8] EUWYCVLU 17: Improve solution for Rust track exercise "Nth Prime".
  • [9] KWE5K6XL 23: Fix panix on input `12` for solution for Rust track exercise "Nth Prime".
  • [10] ZSKHCSQ5 30: Fix regression introduced in solution for Rust track exercise "Nth Prime".

Change contents

  • replacement in rust/nth-prime/src/lib.rs at line 6
    [4.545][4.0:22]()
    * Skips even numbers.
    [4.545]
    [4.0]
    * Odd numbers only (wheel factorisation with base 2).
  • replacement in rust/nth-prime/src/lib.rs at line 22
    [4.264][4.783:896](),[4.783][4.783:896](),[4.896][4.265:350]()
    // Approximated size of the list required using the inverse of an approximation of the prime-counting function.
    let list_size = (corrected_n * (corrected_n * corrected_n.ln()).ln()) as usize + 1;
    [4.264]
    [4.984]
    // Approximated size of the list required using the inverse of an approximation of the prime-counting function, halved (as we're only interested in odd numbers).
    let list_size = (corrected_n * (corrected_n * corrected_n.ln()).ln() / 2.0) as usize + 1;
  • replacement in rust/nth-prime/src/lib.rs at line 31
    [4.1175][3.0:45]()
    [false, true].repeat((list_size / 2) + 1),
    [4.1175]
    [4.23]
    vec![true; list_size],
  • replacement in rust/nth-prime/src/lib.rs at line 35
    [4.123][4.123:161]()
    for index in (3..limit).step_by(2) {
    [4.123]
    [4.1239]
    for index in 1..limit {
  • replacement in rust/nth-prime/src/lib.rs at line 40
    [2.887][4.1369:1394](),[4.1369][4.1369:1394]()
    return index as u32;
    [2.887]
    [4.26]
    return (index * 2 + 1) as u32;
  • replacement in rust/nth-prime/src/lib.rs at line 42
    [4.31][4.162:275]()
    (index.pow(2)..list_size)
    .step_by(index)
    .for_each(|multiple| {
    list[multiple] = false;
    });
    [4.31]
    [4.31]
    for multiple in (((index * 2 + 1).pow(2) - 1) / 2..list_size).step_by(index * 2 + 1) {
    list[multiple] = false;
    }
  • replacement in rust/nth-prime/src/lib.rs at line 47
    [4.38][4.38:39](),[4.39][4.276:399](),[4.399][2.888:950](),[2.950][4.478:548](),[4.478][4.478:548]()
    if limit & 1 == 1 {
    for index in (limit..list_size).step_by(2) {
    if list[index] {
    count += 1;
    if count > n {
    // Might panic if values greater than u32::MAX are cast.
    return index as u32;
    }
    }
    }
    } else {
    if list[limit] {
    [4.38]
    [4.91]
    for index in limit..list_size {
    if list[index] {
  • replacement in rust/nth-prime/src/lib.rs at line 52
    [2.1012][4.549:574](),[4.202][4.549:574]()
    return limit as u32;
    [2.1012]
    [4.574]
    return (index * 2 + 1) as u32;
  • edit in rust/nth-prime/src/lib.rs at line 55
    [4.583][4.583:692](),[4.692][2.1013:1075](),[2.1075][4.771:803](),[4.771][4.771:803](),[4.227][4.1520:1529](),[4.803][4.1520:1529](),[4.1520][4.1520:1529]()
    for index in ((limit + 1)..list_size).step_by(2) {
    if list[index] {
    count += 1;
    if count > n {
    // Might panic if values greater than u32::MAX are cast.
    return index as u32;
    }
    }
    }