Skip to content

Commit

Permalink
add limit, primes docs
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 12, 2024
1 parent 86a4ca3 commit 499a6c8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def setup(app):
{
"fn": {
},
"struct": {
},
"var": {
},
}
Expand Down
9 changes: 9 additions & 0 deletions docs/rust.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ build or test processes.
Alias for ``cargo clean``.

Library Code
------------

.. toctree::
:numbered:
:maxdepth: 1

rust/primes

Problems Solved
---------------

Expand Down
22 changes: 22 additions & 0 deletions docs/rust/primes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
primes.rs
=========

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/rust/src/primes.rs>`_

.. 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<u64>
.. rust:struct:: primes::PrimeFactors
.. rust:fn:: primes::PrimeFactors::new() -> PrimeFactors
.. rust:fn:: primes::PrimeFactors::next() -> Option<u64>
.. literalinclude:: ../../rust/src/p0001.rs
:language: rust
:linenos:
13 changes: 12 additions & 1 deletion rust/src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub struct Eratosthenes {
sieve: HashMap<u64, Vec<u64>>,
prime: u64,
candidate: u64,
limit: u64,
}

impl Default for Eratosthenes {
Expand All @@ -12,6 +13,7 @@ impl Default for Eratosthenes {
sieve: HashMap::new(),
prime: 0,
candidate: 2,
limit: u64::MAX,
};
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 499a6c8

Please sign in to comment.