Skip to content

Commit

Permalink
Revert test addition, mark 357 in progress, make math generics more r…
Browse files Browse the repository at this point in the history
…eadable
  • Loading branch information
LivInTheLookingGlass committed Jul 25, 2024
1 parent c274b84 commit c853a60
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ LivInTheLookingGlass’s Project Euler solutions
| | Pypy 3.6+ |br| | | |Py-Cov| |
| | GraalPy 23.1+ | | |
+------------+-------------------------+--------+-------------------+
| Rust | 1.69+ | 16 | |Rust| |br| |
| Rust | 1.69+ | 15 | |Rust| |br| |
| | | | |Rs-Cov| |
+------------+-------------------------+--------+-------------------+
| Documentation | |Pages| |
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ Key:
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`206`| | | | |:py-d:`0206`| |
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`357`| | | | |:py-s:`0357`|:rs-d:`0357`|
|:prob:`357`| | | | |:py-s:`0357`|:rs-i:`0357`|
+-----------+------------+------------+------------+------------+------------+------------+

.. toctree::
Expand Down
2 changes: 1 addition & 1 deletion rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn main() {
}

#[cfg(test)]
seq!(N in 0..16 {
seq!(N in 0..15 {
#[rstest]
#[timeout(Duration::new(60, 0))]
#(
Expand Down
23 changes: 21 additions & 2 deletions rust/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
use std::cmp::PartialOrd;
use std::mem::size_of;

use num_traits::NumAssign;
use num_traits::one;

pub fn factorial<I: NumAssign + From<u8>>(n: u8) -> I {
const MAX_FACTORIAL: [usize; 16] = [
5, // u8
8, // u16
10, 12, // u32
14, 16, 18, 20, // u64
22, 24, 25, 27, 29, 30, 32, 34 // u128
];

pub fn factorial<I>(n: u8) -> I
where I: NumAssign + From<u8>
{
let mut answer: I = one();
for i in 2..=n {
answer *= i.into();
}
return answer
}

pub fn n_choose_r<I: Copy + From<u64> + NumAssign + PartialOrd>(n: usize, r: usize) -> I {
pub fn n_choose_r<I>(n: usize, r: usize) -> I
where I: Copy + From<u8> + From<u64> + NumAssign + PartialOrd
{
if n < r {
panic!("Out of function's bounds");
}
if n < MAX_FACTORIAL[size_of::<I>() as usize] {
return factorial::<I>(n as u8) / factorial::<I>(r as u8);
}
// slow path for larger numbers
let mut answer: I = one();
let mut tmp: I;
Expand Down
52 changes: 39 additions & 13 deletions rust/src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ use num_traits::zero;
use num_traits::one;
use itertools::Itertools;

pub struct Eratosthenes<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy> {
pub struct Eratosthenes<I>
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
sieve: HashMap<I, Vec<I>>,
prime: I,
candidate: I,
limit: I,
}

impl<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy> Default for Eratosthenes<I> {
impl<I> Default for Eratosthenes<I>
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
fn default() -> Self {
return Eratosthenes::<I>{
sieve: HashMap::new(),
Expand All @@ -26,7 +30,9 @@ impl<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy> Default for Eratost
}
}

impl<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy> Eratosthenes<I> {
impl<I> Eratosthenes<I>
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
pub fn new() -> Eratosthenes<I> {
return Default::default();
}
Expand All @@ -39,7 +45,9 @@ impl<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy> Eratosthenes<I> {
}
}

impl<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy> Iterator for Eratosthenes<I> {
impl<I> Iterator for Eratosthenes<I>
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
type Item = I;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -72,27 +80,37 @@ impl<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy> Iterator for Eratos
}
}

pub fn primes<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy>() -> Eratosthenes<I> {
pub fn primes<I>() -> Eratosthenes<I>
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
return Eratosthenes::new();
}

pub fn primes_until<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy>(x: I) -> Eratosthenes<I> {
pub fn primes_until<I>(x: I) -> Eratosthenes<I>
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
return Eratosthenes::with_limit(x);
}

pub struct PrimeFactors<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy> {
pub struct PrimeFactors<I>
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
number: I
}

impl<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy> PrimeFactors<I> {
impl<I> PrimeFactors<I>
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
pub fn new(x: I) -> PrimeFactors<I> {
return PrimeFactors{
number: x
};
}
}

impl<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy> Iterator for PrimeFactors<I> {
impl<I> Iterator for PrimeFactors<I>
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
type Item = I;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -109,11 +127,15 @@ impl<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy> Iterator for PrimeF
}
}

pub fn prime_factors<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy>(x: I) -> PrimeFactors<I> {
pub fn prime_factors<I>(x: I) -> PrimeFactors<I>
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
return PrimeFactors::new(x);
}

pub fn proper_divisors<I: NumAssign + Bounded + Ord + Eq + Hash + Copy>(x: I) -> Vec<I> {
pub fn proper_divisors<I>(x: I) -> Vec<I>
where I: NumAssign + Bounded + Ord + Eq + Hash + Copy
{
let mut ret: Vec<I> = vec![];
let factors: Vec<I> = PrimeFactors::new(x).collect();
ret.extend(factors.clone());
Expand All @@ -127,7 +149,9 @@ pub fn proper_divisors<I: NumAssign + Bounded + Ord + Eq + Hash + Copy>(x: I) ->
return ret;
}

pub fn is_composite<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy>(x: I) -> I {
pub fn is_composite<I>(x: I) -> I
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
match prime_factors(x).next() {
None => {
return zero();
Expand All @@ -141,6 +165,8 @@ pub fn is_composite<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy>(x: I
}
}

pub fn is_prime<I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy>(x: I) -> bool {
pub fn is_prime<I>(x: I) -> bool
where I: NumAssign + Bounded + PartialOrd + Eq + Hash + Copy
{
return is_composite(x) == zero();
}

0 comments on commit c853a60

Please sign in to comment.