From 452b316777cab0b90275d2780fac8d8011126fdf Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Sun, 14 Jul 2024 12:29:53 -0500 Subject: [PATCH] Fix typo in main, p0007, solve p0010 --- README.rst | 2 +- docs/index.rst | 2 +- docs/rust/p0010.rst | 18 ++++++++++++++++++ rust/src/main.rs | 6 ++++-- rust/src/p0007.rs | 7 +------ rust/src/p0010.rs | 5 +++++ 6 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 docs/rust/p0010.rst create mode 100644 rust/src/p0010.rs diff --git a/README.rst b/README.rst index 8154b8f4..7a2636b4 100644 --- a/README.rst +++ b/README.rst @@ -42,7 +42,7 @@ LivInTheLookingGlass’s Project Euler solutions | | Pypy 3.6+ |br| | | | | | GraalPy 23.1+ | | | +------------+---------------------+--------------+---------------+ -| Rust | 1.69+ | 4 / |total| | |Rust| | +| Rust | 1.69+ | 5 / |total| | |Rust| | +------------+---------------------+--------------+---------------+ | Documentation (in progress) | |Pages| | +-------------------------------------------------+---------------+ diff --git a/docs/index.rst b/docs/index.rst index b85bb5ae..e0c7cfd4 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -112,7 +112,7 @@ This project is divided into several Makefiles, connected by a root Makefile whi +-----------+------+------+------+------+------+ |:prob:`9` | |d| | | | |d| | | +-----------+------+------+------+------+------+ -|:prob:`10` | |d| | | | |d| | | +|:prob:`10` | |d| | | | |d| | |d| | +-----------+------+------+------+------+------+ |:prob:`11` | |d| | | | |d| | | +-----------+------+------+------+------+------+ diff --git a/docs/rust/p0010.rst b/docs/rust/p0010.rst new file mode 100644 index 00000000..d79b8ba8 --- /dev/null +++ b/docs/rust/p0010.rst @@ -0,0 +1,18 @@ +Rust Implementation of Problem 10 +================================= + +View source code `here on GitHub! `_ + +Includes +-------- + +- `primes <./primes.html>`_ + +Problem Solution +---------------- + +.. rust:fn:: p0010::p0010() -> u64 + +.. literalinclude:: ../../rust/src/p0010.rs + :language: rust + :linenos: diff --git a/rust/src/main.rs b/rust/src/main.rs index dd576654..d1e35692 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -13,15 +13,17 @@ seq!(N in 0001..=0003 { mod p~N; }); mod p0007; +mod p0010; mod primes; type ProblemType = fn() -> u64; type ProblemRef<'a> = (&'a str, ProblemType, u64); -const ANSWERS: [ProblemRef; 4] = [ +const ANSWERS: [ProblemRef; 5] = [ ("p0001", p0001::p0001, 233168), ("p0002", p0002::p0002, 4613732), ("p0003", p0003::p0003, 6857), ("p0007", p0007::p0007, 104743), + ("p0010", p0010::p0010, 142913828922), ]; fn main() { @@ -36,7 +38,7 @@ fn main() { } #[cfg(test)] -seq!(N in 0..3 { +seq!(N in 0..=4 { #[rstest] #[timeout(Duration::new(60, 0))] #( diff --git a/rust/src/p0007.rs b/rust/src/p0007.rs index dfd44759..5010ca30 100644 --- a/rust/src/p0007.rs +++ b/rust/src/p0007.rs @@ -12,10 +12,5 @@ What is the 10 001st prime number? use crate::primes::primes; pub fn p0007() -> u64 { - for (i, p) in primes().enumerate() { - if i == 1001 { - return p; - } - } - return 0; + return primes().take(10001).last().unwrap(); } diff --git a/rust/src/p0010.rs b/rust/src/p0010.rs new file mode 100644 index 00000000..af2552c6 --- /dev/null +++ b/rust/src/p0010.rs @@ -0,0 +1,5 @@ +use crate::primes::primes; + +pub fn p0010() -> u64 { + return primes::().take_while(|&p| p < 2000000).sum(); +}