struct Num { rdigits: Vec } impl Num { pub fn from(s: &String) -> Num { let rd: Vec = s.chars().rev() .map(|c| String::from(c).parse::().unwrap()) .collect(); Num { rdigits: rd } } pub fn fromn(n: u32) -> Num { let mut rd = Vec::::new(); let mut nn = n; while nn > 0 { let d = (nn % 10) as u8; rd.push(d); nn /= 10; } Num { rdigits: rd } } pub fn add(&self, n: &Num) -> Num { let mut xs = self.rdigits.clone(); let mut ys = n.rdigits.clone(); let mut rs: Vec = Vec::new(); while xs.len() < ys.len() { xs.push(0); } while ys.len() < xs.len() { ys.push(0); } let mut rm = 0u8; for (a, b) in xs.iter().zip(ys.iter()) { if a + b + rm > 9 { rs.push((a + b + rm) - 10); rm = 1u8; } else { rs.push(a + b + rm); rm = 0u8; } } if rm > 0 { rs.push(1u8); } Num { rdigits: rs } } pub fn mult(&self, n: u32) -> Num { let mut res = self.add(&Num::fromn(0)); for n in 1..n { res = res.add(&self); } return res; } pub fn print_10(&self) { for d in self.rdigits.iter().rev().take(10) { print!("{}", d); } //println!(""); } } struct D { a: Num, b: Num } impl D { pub fn from(a: Num, b: Num) -> D { D { a: a, b: b } } pub fn print(&self) { self.a.print_10(); print!("/"); self.b.print_10(); println!(""); } pub fn add_n(&self, n: u32) -> D { let nn = self.b.mult(n); return D::from(nn.add(&self.a), self.b.add(&Num::fromn(0))); } pub fn iter(&self) -> D { let mut res = self.add_n(1); let dd = D::from(res.b, res.a); return dd.add_n(1); } pub fn bigger(&self) -> bool { return self.a.rdigits.len() > self.b.rdigits.len() } } fn main() { let mut d = D::from(Num::fromn(1), Num::fromn(2)); let mut count = 0; d = d.add_n(1); //d.print(); for n in 0..1000 { d = d.iter(); //d.print(); if d.bigger() { count += 1; } } println!("{}", count); }