-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add partial rust prime infrastructure
- Loading branch information
1 parent
e5927df
commit 03513b9
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ venv/** | |
csharp/*/obj | ||
csharp/*/bin | ||
**/TestResults | ||
docs/_build | ||
docs/_build | ||
rust/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |