Skip to content

Instantly share code, notes, and snippets.

@lotabout
Created May 20, 2016 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lotabout/0236e5d994be3f34a20cc7dc26c8219a to your computer and use it in GitHub Desktop.
Save lotabout/0236e5d994be3f34a20cc7dc26c8219a to your computer and use it in GitHub Desktop.
fn gen_wheel(primes: Vec<u64>) -> Box<std::iter::Iterator<Item = u64>> {
let mul = primes.iter().fold(1, |acc, x| acc * x);
let stroke_left: Vec<_>= (2..(mul+1))
.filter(|x| primes.iter().all(|&y| x % y != 0))
.collect();
let front_wheel: Vec<_> = vec![0].iter().chain(primes.iter())
.zip(primes.iter().chain(stroke_left.iter().take(1)))
.map(|(&x, &y)| y - x)
.collect();
let diff_wheel: Vec<_>= vec![1].iter().chain(stroke_left.iter())
.zip(stroke_left.iter().chain(vec![mul+1].iter()))
.map(|(&x, &y)| y - x)
.collect();
Box::new(front_wheel.into_iter().chain(diff_wheel.into_iter().cycle().skip(1)))
}
pub fn main() {
let wheel = gen_wheel(vec![2,3]);
for i in wheel.take(30) {
println!("i = {}", i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment