From 499a6c825143e8889dddbff0868ed07f9b49bea0 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Thu, 11 Jul 2024 20:48:52 -0500 Subject: [PATCH] add limit, primes docs --- docs/conf.py | 2 ++ docs/rust.rst | 9 +++++++++ docs/rust/primes.rst | 22 ++++++++++++++++++++++ rust/src/primes.rs | 13 ++++++++++++- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 docs/rust/primes.rst diff --git a/docs/conf.py b/docs/conf.py index 9dc38ee8..75b35294 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -80,6 +80,8 @@ def setup(app): { "fn": { }, + "struct": { + }, "var": { }, } diff --git a/docs/rust.rst b/docs/rust.rst index 4bd7d9b4..d3108d0d 100644 --- a/docs/rust.rst +++ b/docs/rust.rst @@ -34,6 +34,15 @@ build or test processes. Alias for ``cargo clean``. +Library Code +------------ + +.. toctree:: + :numbered: + :maxdepth: 1 + + rust/primes + Problems Solved --------------- diff --git a/docs/rust/primes.rst b/docs/rust/primes.rst new file mode 100644 index 00000000..57b63a7d --- /dev/null +++ b/docs/rust/primes.rst @@ -0,0 +1,22 @@ +primes.rs +========= + +View source code `here on GitHub! `_ + +.. rust:struct:: primes::Eratosthenes + + .. rust:fn:: primes::Eratosthenes::new() -> Eratosthenes + + .. rust:fn:: primes::Eratosthenes::with_limit(limit: u64) -> Eratosthenes + + .. rust:fn:: primes::Eratosthenes::next() -> Option + +.. rust:struct:: primes::PrimeFactors + + .. rust:fn:: primes::PrimeFactors::new() -> PrimeFactors + + .. rust:fn:: primes::PrimeFactors::next() -> Option + +.. literalinclude:: ../../rust/src/p0001.rs + :language: rust + :linenos: diff --git a/rust/src/primes.rs b/rust/src/primes.rs index 9829ae3e..6c537bff 100644 --- a/rust/src/primes.rs +++ b/rust/src/primes.rs @@ -4,6 +4,7 @@ pub struct Eratosthenes { sieve: HashMap>, prime: u64, candidate: u64, + limit: u64, } impl Default for Eratosthenes { @@ -12,6 +13,7 @@ impl Default for Eratosthenes { sieve: HashMap::new(), prime: 0, candidate: 2, + limit: u64::MAX, }; } } @@ -20,6 +22,12 @@ impl Eratosthenes { pub fn new() -> Eratosthenes { return Default::default(); } + + pub fn with_limit(limit: u64) -> Eratosthenes { + let mut ret: Eratosthenes = Default::default(); + ret.limit = limit; + return ret; + } } impl Iterator for Eratosthenes { @@ -48,7 +56,10 @@ impl Iterator for Eratosthenes { self.prime = next_prime(&mut self.sieve, self.candidate); self.candidate = self.prime + 1; // This number will be the next to be tested - return Some(self.prime) + if self.prime > self.limit { + return None; + } + return Some(self.prime); } }