Created
May 29, 2020 15:20
-
-
Save lovasoa/201728e965d971301376aff06e0b3dfe to your computer and use it in GitHub Desktop.
3n+1 conjecture in generic rust
This file contains hidden or 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
use num::{Integer}; | |
use std::ops::{MulAssign, DivAssign, AddAssign}; | |
struct Collatz<T>(T); | |
impl<T> Iterator for Collatz<T> | |
where T: Integer + MulAssign + DivAssign + AddAssign + Clone, | |
{ | |
type Item = T; | |
fn next(&mut self) -> Option<T> { | |
if self.0.is_one() || self.0.is_zero() { return None } | |
if self.0.is_even() { | |
self.0 /= T::one() + T::one(); | |
} else { | |
self.0 *= T::one() + T::one() + T::one(); | |
self.0 += T::one(); | |
} | |
Some(self.0.clone()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment