Skip to content

Commit

Permalink
Solve 36, 37, 45, 53 in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 26, 2024
1 parent 4d65f88 commit 7b4fd51
Show file tree
Hide file tree
Showing 24 changed files with 436 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ jobs:
if: ${{ matrix.toolchain != 'nightly' }}
env:
RUSTFLAGS: '-Copt-level=2'
RUST_BACKTRACE: 1

- run: cd rust && cargo test
if: ${{ matrix.toolchain == 'nightly' }}
env:
CARGO_INCREMENTAL: '0'
RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests -Copt-level=1'
RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests -Copt-level=1'
RUST_BACKTRACE: 1

- name: rust-grcov
if: ${{ matrix.toolchain == 'nightly' }}
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Olivia's Project Euler Solutions
| | GraalPy 23.1+ |br| | | |CodeQL| |br| |
| | Browser [#]_ | | |PythonLint| |
+------------+----------------------------+--------+-------------------+
| Rust | 1.69+ |br| | 29 | |Rust| |br| |
| Rust | 1.69+ |br| | 33 | |Rust| |br| |
| | Browser [#]_ | | |Rs-Cov| |br| |
| | | | |RustClippy| |
+------------+----------------------------+--------+-------------------+
Expand Down
8 changes: 4 additions & 4 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ Problems Solved
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`35` | | | | |:js-d:`0035`|:py-d:`0035`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`36` | | | | | |:py-d:`0036`| |
|:prob:`36` | | | | | |:py-d:`0036`|:rs-d:`0036`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`37` | | | | |:js-d:`0037`|:py-d:`0037`| |
|:prob:`37` | | | | |:js-d:`0037`|:py-d:`0037`|:rs-s:`0037`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`38` | | | | | |:py-d:`0038`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand All @@ -162,7 +162,7 @@ Problems Solved
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`44` | | | | | |:py-d:`0044`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`45` | | | | | |:py-d:`0045`| |
|:prob:`45` | | | | | |:py-d:`0045`|:rs-d:`0045`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`46` | | | | |:js-d:`0046`|:py-d:`0046`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand All @@ -176,7 +176,7 @@ Problems Solved
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`52` | | | | | |:py-d:`0052`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`53` | | | | | |:py-d:`0053`| |
|:prob:`53` | | | | | |:py-d:`0053`|:rs-d:`0053`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`55` | | | | | |:py-d:`0055`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
35 changes: 35 additions & 0 deletions docs/src/rust/lib/fibonacci.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
fibonacci.rs
=============

View source code :source:`rust/src/include/fibonacci.rs`

Includes
--------

use std::ops::{Add,Mul};

use num_traits::{one,zero,One,Zero};

use crate::include::iter_cache::cache_iterator;

- :external:rust:trait:`num_traits::One`
- :external:rust:trait:`num_traits::Zero`
- :external:rust:fn:`num_traits::one`
- :external:rust:fn:`num_traits::zero`
- :external:rust:trait:`std::ops::Add`
- :external:rust:trait:`std::ops::Mul`

.. rust:struct:: pub struct fibonacci::CachingIterator<I, T> where I: Iterator<Item = T> + 'static, T: Clone + 'static
This struct implements a global cache that maps an iterator-iterated pair to a cache of values. It is mostly
intended for use with the prime function generator.

.. rust:fn:: pub fn fibonacci::CachingIterator::new() -> CachingIterator<I, T> where I: Iterator<Item = T> + 'static, T: Clone + 'static
.. rust:fn:: pub fn fibonacci::CachingIterator::default() -> CachingIterator<I, T> where I: Iterator<Item = T> + 'static, T: Clone + 'static
.. rust:fn:: pub fn fibonacci::CachingIterator::next() -> Option<T> where T: Clone + 'static
.. literalinclude:: ../../../../rust/src/include/fibonacci.rs
:language: rust
:linenos:
20 changes: 20 additions & 0 deletions docs/src/rust/p0036.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Rust Implementation of Problem 36
=================================

View source code :source:`rust/src/p0036.rs`

Includes
--------

- `math <./lib/math.html>`_

Problem Solution
----------------

.. rust:fn:: pub fn p0036::p0036() -> utils::Answer
.. literalinclude:: ../../../rust/src/p0036.rs
:language: rust
:linenos:

.. tags:: palindrome, number-base
20 changes: 20 additions & 0 deletions docs/src/rust/p0037.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Rust Implementation of Problem 37
=================================

View source code :source:`rust/src/p0037.rs`

Includes
--------

- `math <./lib/math.html>`_

Problem Solution
----------------

.. rust:fn:: pub fn p0037::p0037() -> utils::Answer
.. literalinclude:: ../../../rust/src/p0037.rs
:language: rust
:linenos:

.. tags:: prime-number, digit-manipulation, rust-iterator
20 changes: 20 additions & 0 deletions docs/src/rust/p0045.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Rust Implementation of Problem 45
=================================

View source code :source:`rust/src/p0045.rs`

Includes
--------

- `math <./lib/math.html>`_

Problem Solution
----------------

.. rust:fn:: pub fn p0045::p0045() -> utils::Answer
.. literalinclude:: ../../../rust/src/p0045.rs
:language: rust
:linenos:

.. tags:: figurate-number, triangle-number
20 changes: 20 additions & 0 deletions docs/src/rust/p0053.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Rust Implementation of Problem 53
=================================

View source code :source:`rust/src/p0053.rs`

Includes
--------

- `math <./lib/math.html>`_

Problem Solution
----------------

.. rust:fn:: pub fn p0053::p0053() -> utils::Answer
.. literalinclude:: ../../../rust/src/p0053.rs
:language: rust
:linenos:

.. tags:: combinatorics, factorial, binomial-coefficient
4 changes: 4 additions & 0 deletions rust/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ Problems Solved
- ☒ `24 <./src/p0024.rs>`__
- ☒ `27 <./src/p0027.rs>`__
- ☒ `34 <./src/p0034.rs>`__
- ☒ `36 <./src/p0036.rs>`__
- ☒ `37 <./src/p0037.rs>`__
- ☒ `45 <./src/p0045.rs>`__
- ☒ `53 <./src/p0053.rs>`__
- ☒ `69 <./src/p0069.rs>`__
- ☒ `76 <./src/p0076.rs>`__
- ☒ `77 <./src/p0077.rs>`__
Expand Down
8 changes: 4 additions & 4 deletions rust/src/include/factors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct ProperDivisors<I>
next_size: usize,
}

pub fn proper_divisors<I>(num: I) -> ProperDivisors<I>
pub fn proper_divisors<I>(num: I) -> impl Iterator<Item = I>
where I: Hash + Zero + One + Add + Ord + Copy + Div<Output=I> + Rem<Output=I> + 'static
{
return ProperDivisors::<I>::new(num);
Expand Down Expand Up @@ -48,9 +48,9 @@ where I: Hash + Zero + One + Add + Ord + Copy + Div<Output=I> + Rem<Output=I> +
return None;
}
if self.next_size == 0 {
self.next_size += 1;
return Some(one());
}
self.next_size += 1;
return Some(one());
}
while self.curr_index < self.current_batch.len() {
let result = self.current_batch[self.curr_index];
self.curr_index += 1;
Expand Down
80 changes: 80 additions & 0 deletions rust/src/include/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::ops::{Add,Mul};

use num_traits::{one,zero,One,Zero};

use crate::include::iter_cache::cache_iterator;

pub fn fib<I>() -> impl Iterator<Item = I> where I: Copy + Zero + One + Add + 'static {
return cache_iterator(Fibonacci::<I>::new());
}

pub fn fib_by_3<I>() -> impl Iterator<Item = I> where I: Copy + Zero + One + Add + Mul + 'static {
return cache_iterator(FibonacciBy3::<I>::new());
}

pub struct Fibonacci<I> {
a: I,
b: I,
}

impl<I> Default for Fibonacci<I> where I: Zero + One {
fn default() -> Self {
return Fibonacci::<I>{
a: zero(),
b: one(),
};
}
}

impl<I> Fibonacci<I> where I: Zero + One {
pub fn new() -> Self {
return Default::default();
}
}

impl<I> Iterator for Fibonacci<I> where I: Zero + One + Add + Copy {
type Item = I;

fn next(&mut self) -> Option<Self::Item> {
let prior_a = self.a;
let prior_b = self.b;
self.b = self.a + self.b;
self.a = prior_b;
return Some(prior_a);
}
}

pub struct FibonacciBy3<I> {
a: I,
b: I,
}

impl<I> Default for FibonacciBy3<I> where I: Zero + One + Add + Copy {
fn default() -> Self {
let two = one::<I>() + one();
return FibonacciBy3::<I>{
a: zero(),
b: two,
};
}
}

impl<I> FibonacciBy3<I> where I: Zero + One + Add + Copy {
pub fn new() -> Self {
return Default::default();
}
}

impl<I> Iterator for FibonacciBy3<I> where I: Zero + One + Add + Mul + Copy {
type Item = I;

fn next(&mut self) -> Option<Self::Item> {
let two = one::<I>() + one();
let four = two + two;
let prior_a = self.a;
let prior_b = self.b;
self.b = four * self.b + self.a;
self.a = prior_b;
return Some(prior_a);
}
}
8 changes: 8 additions & 0 deletions rust/src/include/iter_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ use std::sync::Once;
static INIT: Once = Once::new();
static mut CACHE_MAP: Option<RwLock<HashMap<(TypeId, TypeId), Mutex<Vec<Box<dyn std::any::Any>>>>>> = None;

pub fn cache_iterator<I, T>(iterator: I) -> impl Iterator<Item = T>
where
I: Iterator<Item = T> + 'static,
T: Copy + 'static
{
return CachingIterator::new(iterator);
}

pub struct CachingIterator<I, T>
where
I: Iterator<Item = T> + 'static,
Expand Down
1 change: 1 addition & 0 deletions rust/src/include/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod iter_cache;
pub mod factors;
pub mod fibonacci;
pub mod math;
pub mod primes;
pub mod problems;
Expand Down
37 changes: 12 additions & 25 deletions rust/src/include/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ use std::ops::{Add,Div,Mul,Rem};

use num_traits::{one,zero,One,Zero};

use crate::include::iter_cache::CachingIterator;
use crate::include::iter_cache::cache_iterator;

pub struct Eratosthenes<I>
where I: Hash
{
pub struct Eratosthenes<I> where I: Hash {
sieve: HashMap<I, Vec<I>>,
prime: I,
candidate: I,
}

impl<I> Default for Eratosthenes<I>
where I: Hash + One + Zero + Add
{
impl<I> Default for Eratosthenes<I> where I: Hash + One + Zero + Add {
fn default() -> Self {
return Eratosthenes::<I>{
sieve: HashMap::new(),
Expand All @@ -27,17 +23,13 @@ where I: Hash + One + Zero + Add
}
}

impl<I> Eratosthenes<I>
where I: Hash + One + Zero + Add
{
impl<I> Eratosthenes<I> where I: Hash + One + Zero + Add {
pub fn new() -> Eratosthenes<I> {
return Default::default();
}
}

impl<I> Iterator for Eratosthenes<I>
where I: Hash + One + Zero + Add + Mul + Ord + Copy
{
impl<I> Iterator for Eratosthenes<I> where I: Hash + One + Zero + Add + Mul + Ord + Copy {
type Item = I;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -68,25 +60,19 @@ where I: Hash + One + Zero + Add + Mul + Ord + Copy
}
}

pub fn primes<I>() -> impl Iterator<Item = I>
where I: Hash + One + Zero + Add + Mul + Ord + Copy + 'static
{
return CachingIterator::new(Eratosthenes::new());
pub fn primes<I>() -> impl Iterator<Item = I> where I: Hash + One + Zero + Add + Mul + Ord + Copy + 'static {
return cache_iterator(Eratosthenes::new());
}

pub fn primes_until<I>(x: I) -> impl Iterator<Item = I>
where I: Hash + One + Zero + Add + Mul + Ord + Copy + 'static
{
pub fn primes_until<I>(x: I) -> impl Iterator<Item = I> where I: Hash + One + Zero + Add + Mul + Ord + Copy + 'static {
return primes::<I>().take_while(move |n| *n < x);
}

pub struct PrimeFactors<I>
{
pub struct PrimeFactors<I> {
number: I
}

impl<I> PrimeFactors<I>
{
impl<I> PrimeFactors<I> {
pub fn new(x: I) -> PrimeFactors<I> {
return PrimeFactors{
number: x
Expand All @@ -113,7 +99,8 @@ where I: Hash + Zero + One + Add + Ord + Copy + Div<Output=I> + Rem<Output=I> +
}
}

pub fn prime_factors<I>(x: I) -> PrimeFactors<I>
pub fn prime_factors<I>(x: I) -> impl Iterator<Item = I>
where I: Hash + Zero + One + Add + Ord + Copy + Div<Output=I> + Rem<Output=I> + 'static
{
return PrimeFactors::new(x);
}
Expand Down
Loading

0 comments on commit 7b4fd51

Please sign in to comment.