Loading RustViz Engine...
Implement an infinite Collatz conjecture sequence generator using .take_while().
This problem is currently in blueprint template mode. Once the concept foundation is finalized, live in-browser WebAssembly compilation and test assertion runners will be enabled here.
pub struct Collatz {
curr: u64,
}
impl Iterator for Collatz {
type Item = u64;
fn next(&mut self) -> Option<Self::Item> {
if self.curr == 0 { return None; }
let val = self.curr;
if self.curr == 1 { self.curr = 0; }
else if self.curr % 2 == 0 { self.curr /= 2; }
else { self.curr = 3 * self.curr + 1; }
Some(val)
}
}