add 200 (WIP)
This commit is contained in:
@@ -106,6 +106,10 @@ where
|
|||||||
pub fn iter(&self) -> Iter<T> {
|
pub fn iter(&self) -> Iter<T> {
|
||||||
self.vector.iter()
|
self.vector.iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn into_vec(self) -> Vec<T> {
|
||||||
|
self.vector
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "euler200"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
primes = {version = "*", path = "../../lib/primes"}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
extern crate primes;
|
||||||
|
use primes::Primes;
|
||||||
|
use std::{collections::HashSet, vec::IntoIter};
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
const MAX_VALUE: u64 = 100;
|
||||||
|
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
const MAX_VALUE: u64 = 10_000_000;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut squbes = Squbes::new(MAX_VALUE);
|
||||||
|
squbes.iterate();
|
||||||
|
squbes.iterate();
|
||||||
|
squbes.iterate();
|
||||||
|
println!("{:?}", squbes.output());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Squbes {
|
||||||
|
squbes: HashSet<u64>,
|
||||||
|
new_squbes: Vec<u64>,
|
||||||
|
primes: IntoIter<u64>,
|
||||||
|
used_primes: Vec<u64>,
|
||||||
|
current_prime: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Squbes {
|
||||||
|
pub fn new(max: u64) -> Self {
|
||||||
|
let mut primes = Primes::get_between(2, max).into_vec().into_iter();
|
||||||
|
let first = primes.next().unwrap();
|
||||||
|
let second = primes.next().unwrap();
|
||||||
|
Self {
|
||||||
|
squbes: HashSet::new(),
|
||||||
|
new_squbes: vec![],
|
||||||
|
primes,
|
||||||
|
used_primes: vec![first],
|
||||||
|
current_prime: second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn iterate(&mut self) {
|
||||||
|
self.calculate_squbes_with_current_prime();
|
||||||
|
self.save_current_prime();
|
||||||
|
self.update_current_prime();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn output(&self) -> Vec<u64> {
|
||||||
|
let mut result: Vec<_> = self.squbes.iter().copied().collect();
|
||||||
|
result.sort_unstable();
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_current_prime(&mut self) {
|
||||||
|
self.used_primes.push(self.current_prime);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_current_prime(&mut self) {
|
||||||
|
self.current_prime = self.primes.next().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_squbes_with_current_prime(&mut self) {
|
||||||
|
assert_ne!(self.current_prime, 0);
|
||||||
|
let iter = self
|
||||||
|
.used_primes
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.zip([self.current_prime].into_iter().cycle())
|
||||||
|
.chain(
|
||||||
|
[self.current_prime]
|
||||||
|
.into_iter()
|
||||||
|
.cycle()
|
||||||
|
.zip(self.used_primes.iter().copied()),
|
||||||
|
)
|
||||||
|
.map(|(p, q)| Self::core(p, q));
|
||||||
|
self.new_squbes.extend(iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn core(p: u64, q: u64) -> u64 {
|
||||||
|
p.pow(2) * q.pow(3)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user