Skip to content

Commit

Permalink
fix p23 rust
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 24, 2024
1 parent 3bc057e commit c700d2f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
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| | 28 | |Rust| |br| |
| Rust | 1.69+ |br| | 29 | |Rust| |br| |
| | Browser [#]_ | | |Rs-Cov| |br| |
| | | | |RustClippy| |
+------------+----------------------------+--------+-------------------+
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Problems Solved
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`22` |:c-d:`0022` |:cp-d:`0022`|:cs-d:`0022`|:ja-d:`0022`|:js-d:`0022`|:py-d:`0022`|:rs-d:`0022`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`23` | | | | |:js-d:`0023`|:py-d:`0023`|:rs-i:`0023`|
|:prob:`23` | | | | |:js-d:`0023`|:py-d:`0023`|:rs-d:`0023`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`24` | | | | | |:py-d:`0024`|:rs-d:`0024`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
24 changes: 12 additions & 12 deletions rust/src/include/factors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ where I: Hash + Zero + One + Add + Ord + Copy + Div<Output=I> + Rem<Output=I> +
factors: prime_factors(num).collect(),
current_batch: vec![],
curr_index: 0,
next_size: 1,
next_size: 0,
};
}
}
Expand All @@ -47,16 +47,10 @@ where I: Hash + Zero + One + Add + Ord + Copy + Div<Output=I> + Rem<Output=I> +
if self.next_size > self.factors.len() {
return None;
}
if self.curr_index == self.current_batch.len() {
self.current_batch = self.factors
.iter()
.cloned()
.combinations(self.next_size)
.map(|v| v.into_iter().fold(one(), |x, y| x * y))
.collect();
self.next_size += 1;
self.curr_index = 0;
}
if self.next_size == 0 {
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 All @@ -65,7 +59,13 @@ where I: Hash + Zero + One + Add + Ord + Copy + Div<Output=I> + Rem<Output=I> +
return Some(result);
}
}
self.current_batch.clear();
self.current_batch = self.factors
.iter()
.cloned()
.combinations(self.next_size)
.map(|v| v.into_iter().fold(one(), |x, y| x * y))
.collect();
self.next_size += 1;
self.curr_index = 0;
}
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/include/problems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn get_problem<'b>(n: usize) -> Option<ProblemRef<'b>> {
20 => Some(( &20, p0020, false)),
21 => Some(( &21, p0021, true)),
22 => Some(( &22, p0022, false)),
23 => Some(( &23, p0023, true)),
23 => Some(( &23, p0023, false)),
24 => Some(( &24, p0024, false)),
27 => Some(( &27, p0027, true)),
34 => Some(( &34, p0034, false)),
Expand All @@ -66,4 +66,4 @@ pub fn generate_supported_problems(include_slow: bool) -> Vec<usize> {
}

return supported_problems;
}
}
8 changes: 6 additions & 2 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use itertools::Itertools;

pub mod include;
use include::primes;
use include::factors;
#[cfg(not(test))]
use include::problems::generate_supported_problems;
use include::problems::get_problem;
Expand All @@ -39,6 +40,9 @@ fn main() {
for i in sieve {
println!("{}", i);
}
for i in 4..100 {
println!("{}: {:?}", i, factors::proper_divisors(i).collect::<Vec<u16>>());
}
let supported = generate_supported_problems(false);

for id in supported {
Expand All @@ -63,9 +67,9 @@ seq!(N in 01..=20 {
#(
#[case::problem_~N(N)]
)*
// #[case::problem_21(21)]
//#[case::problem_21(21)]
#[case::problem_22(22)]
// #[case::problem_23(23)]
#[case::problem_23(23)]
#[case::problem_24(24)]
#[case::problem_27(27)]
#[case::problem_34(34)]
Expand Down
12 changes: 5 additions & 7 deletions rust/src/p0023.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ two abundant numbers.
*/
use std::collections::HashSet;

use itertools::Itertools;

use crate::include::factors::proper_divisors;
use crate::include::utils::Answer;

Expand All @@ -37,14 +35,14 @@ pub fn p0023() -> Answer {
let mut abundants: Vec<u64> = vec![];

for x in 12..28112 {
if proper_divisors(x).sum::<u64>() > x {
if proper_divisors::<u64>(x).sum::<u64>() > x {
abundants.push(x);
}
}
for v in abundants.into_iter().combinations_with_replacement(2) {
let x = v[0];
let y = v[1];
abundant_sums.insert(x + y);
for x in abundants.clone().into_iter() {
for y in &abundants {
abundant_sums.insert(x + y);
}
}
let mut sum = 0;
for x in 1..28124 {
Expand Down

0 comments on commit c700d2f

Please sign in to comment.