34: Improve solution for Rust track exercise "Nth Prime".
[?]
Aaw9nJhsNmfzFih9mKyNw9mV8CgERXJkRa1kK1Kx3LQH
Sep 3, 2021, 12:04 PM
YGJVWTKAANDS6TUXWRI5V4TLVWTZNBNX2HXRYJ2WSPBPI2U6MB2ACDependencies
- [2]
XDEY7SNL31: Improve solution for Rust track exercise "Raindrops". - [3]
VH5OFL5532: Improve solution for Rust track exercise "Nth Prime". - [4]
JZN2AQ3E10: Add Rust track exercise "nth-prime". - [5]
ERYD4CD711: Add solution for Rust track exercise "Nth Prime". - [6]
IXMBS6ZP29: Improve solution for Rust track exercise "Nth Prime". - [7]
UTP7D53Q12: Improve solution for Rust track exercise "Nth Prime". - [8]
EUWYCVLU17: Improve solution for Rust track exercise "Nth Prime". - [9]
KWE5K6XL23: Fix panix on input `12` for solution for Rust track exercise "Nth Prime". - [10]
ZSKHCSQ530: Fix regression introduced in solution for Rust track exercise "Nth Prime".
Change contents
- replacement in rust/nth-prime/src/lib.rs at line 6
* Skips even numbers.* Odd numbers only (wheel factorisation with base 2). - replacement in rust/nth-prime/src/lib.rs at line 22
// 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;// 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
[false, true].repeat((list_size / 2) + 1),vec![true; list_size], - replacement in rust/nth-prime/src/lib.rs at line 35
for index in (3..limit).step_by(2) {for index in 1..limit { - replacement in rust/nth-prime/src/lib.rs at line 40
return index as u32;return (index * 2 + 1) as u32; - replacement in rust/nth-prime/src/lib.rs at line 42
(index.pow(2)..list_size).step_by(index).for_each(|multiple| {list[multiple] = false;});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] {for index in limit..list_size {if list[index] { - replacement in rust/nth-prime/src/lib.rs at line 52
return limit as u32;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;}}}