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.
56 lines
1.4 KiB
56 lines
1.4 KiB
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 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 len(&self) -> usize { self.rdigits.len() } |
|
pub fn print(&self) { |
|
for c in self.rdigits.iter().rev() { print!("{}", c); }; |
|
println!(""); |
|
} |
|
} |
|
|
|
|
|
fn main() { |
|
let mut fa = Num::from(&String::from("1")); |
|
let mut fb = Num::from(&String::from("1")); |
|
let mut i = 2; |
|
let l = 1000usize; |
|
|
|
while fb.len() < l { |
|
let fc = fa.add(&fb); |
|
fa = fb; |
|
fb = fc; |
|
i += 1; |
|
} |
|
fb.print(); |
|
println!("-----------"); |
|
println!("[{}]", i); |
|
|
|
}
|
|
|