Skip to content

Commit

Permalink
Add partial rust prime infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 9, 2024
1 parent e5927df commit 03513b9
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ venv/**
csharp/*/obj
csharp/*/bin
**/TestResults
docs/_build
docs/_build
rust/target
19 changes: 19 additions & 0 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[cfg(test)]
use std::time::Duration;
// #[cfg(test)]
// use std::iter::zip;

use seq_macro::seq;
#[cfg(test)]
Expand All @@ -8,6 +10,7 @@ use rstest::rstest;
seq!(N in 0001..=0002 {
mod p~N;
});
mod primes;

type ProblemType = fn() -> u64;
type ProblemRef<'a> = (&'a str, ProblemType, u64);
Expand All @@ -17,6 +20,10 @@ const ANSWERS: [ProblemRef; 2] = [
];

fn main() {
// let sieve = primes::ModifiedEratosthenes::new().take(10);
// for i in sieve {
// println!("{}", i);
// }
for (name, func, answer) in ANSWERS {
let result = func();
println!("Problem {} should return {}. Returned {}!", name, answer, result);
Expand All @@ -37,3 +44,15 @@ fn test_problem(#[case] idx: usize) -> Result<(), String> {
Ok(())
}
});


// #[cfg(test)]
// #[test]
// fn test_primes() -> Result<(), String> {
// let primes = [2, 3, 5, 7, 11, 13, 17, 19];
// let sieve = primes::ModifiedEratosthenes::new().take(primes.len());
// for (p, s) in zip(primes, sieve) {
// assert_eq!(p, s);
// }
// Ok(())
// }
66 changes: 66 additions & 0 deletions rust/src/primes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::collections::HashMap;

pub struct ModifiedEratosthenes {
sieve: HashMap<u64, u64>,
prime: u64,
candidate: u64,
recurse: Option<Box<ModifiedEratosthenes>>
}

impl ModifiedEratosthenes {
pub fn new() -> ModifiedEratosthenes {
return ModifiedEratosthenes{
sieve: HashMap::new(),
prime: 0,
candidate: 2,
recurse: None
};
}
}

impl Iterator for ModifiedEratosthenes {
type Item = u64;

fn next(&mut self) -> Option<Self::Item> {
if self.candidate == 2 {
self.candidate = 3;
self.prime = 3;
println!("Returning 2");
return Some(2);
}
let mut candidate = self.candidate;
self.candidate += 2;
loop {
let prime_squared = self.prime * self.prime;
println!("Candidate: {}", candidate);
let step: u64;
if self.sieve.contains_key(&candidate) {
step = self.sieve.remove(&candidate)?;
println!("Candidate in cache as {}", step);
}
else if candidate < prime_squared {
println!("Candidate not in cache, but less than {}", prime_squared);
return Some(candidate);
}
else {
if candidate != prime_squared {
panic!("Something has gone wrong in the sieve");
}
step = self.prime * 2;
if self.recurse.is_none() {
self.recurse = Some(Box::new(ModifiedEratosthenes::new()));
let mut recursed = self.recurse.take()?;
let _ = (*recursed).next();
}
let mut recursed = self.recurse.take()?;
self.prime = ((*recursed).next())?;
}
println!("This is the good part");
candidate += step;
while self.sieve.contains_key(&candidate) {
candidate += step;
}
self.sieve.insert(candidate, step);
}
}
}

0 comments on commit 03513b9

Please sign in to comment.