diff --git a/020/main.rs b/020/main.rs new file mode 100644 index 0000000..cd758ac --- /dev/null +++ b/020/main.rs @@ -0,0 +1,66 @@ +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 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 clone(&self) -> Num { + Num { rdigits: self.rdigits.clone() } + } + pub fn print(&self) { + for c in self.rdigits.iter().rev() { print!("{}", c); }; + println!(""); + } + pub fn sum(&self) -> u32 { + let mut res = 0u32; + for c in self.rdigits.iter() { res += *c as u32; } + res + } + pub fn mult(&self, n: u32) -> Num { + let dd = self.clone(); + let mut res = Num::from(&String::from("0")); + for i in 1..=n { + res = res.add(&dd); + } + res + } +} + + +fn main() { + let mut n = Num::from(&String::from("1")); + for i in 1..100 { + n = n.mult(i); + } + //let m = n.mult(3); + + n.print(); + //println!("-----------"); + println!("[{}]", n.sum()); + +}