You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.3 KiB

10 months ago
struct Num { rdigits: Vec<u8> }
impl Num {
pub fn from(s: &String) -> Num {
let rd: Vec<u8> = s.chars().rev()
.map(|c| String::from(c).parse::<u8>().unwrap())
.collect();
Num { rdigits: rd }
}
pub fn fromn(n: u32) -> Num {
let mut rd = Vec::<u8>::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<u8> = 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!("");
}
}
10 months ago
struct D {
10 months ago
a: Num,
b: Num
10 months ago
}
impl D {
10 months ago
pub fn from(a: Num, b: Num) -> D {
10 months ago
D { a: a, b: b }
}
pub fn print(&self) {
10 months ago
self.a.print_10();
print!("/");
self.b.print_10();
println!("");
10 months ago
}
pub fn add_n(&self, n: u32) -> D {
10 months ago
let nn = self.b.mult(n);
return D::from(nn.add(&self.a), self.b.add(&Num::fromn(0)));
10 months ago
}
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);
}
10 months ago
pub fn bigger(&self) -> bool {
return self.a.rdigits.len() > self.b.rdigits.len()
}
10 months ago
}
fn main() {
10 months ago
let mut d = D::from(Num::fromn(1), Num::fromn(2));
let mut count = 0;
10 months ago
d = d.add_n(1);
10 months ago
//d.print();
for n in 0..1000 {
10 months ago
d = d.iter();
10 months ago
//d.print();
if d.bigger() {
count += 1;
}
10 months ago
}
10 months ago
println!("{}", count);
10 months ago
}