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 pow(&self, n: u32) -> Num { let mut res = self.add(&Num::fromn(0)); let m = res.as_10(); for n in 1..n { res = res.mult(m); } return res; } pub fn print_10(&self) { for d in self.rdigits.iter().rev().take(10) { print!("{}", d); } println!(""); } pub fn as_10(&self) -> u32 { let mut res = 0u32; assert!(self.rdigits.len() < 8); for d in self.rdigits.iter().rev() { res *= 10; res += *d as u32; } return res; } pub fn dsum(&self) -> u64 { let mut res = 0u64; for d in self.rdigits.iter() { res += *d as u64; } return res; } } fn main() { let mut best_sum = 0u64; for a in 1..100 { for b in 1..100 { let mut t = Num::fromn(a); t = t.pow(b); //let k = t.as_10(); //println!("{}", k); //t.print_10(); let dsum = t.dsum(); println!("{}^{} = {}", a, b, dsum); if dsum > best_sum { best_sum = dsum; } } } println!("{}", best_sum); }