use crate::Solution;
use pmath::newtons_method;
problem!(Problem0030, 30, "Digit Fifth Powers");
impl Solution for Problem0030 {
fn solve(&self) -> String {
let max_digits = newtons_method(
10.0,
1e-10,
|n| 10.0_f64.powf(n) - n * (9_u32.pow(5) as f64),
|n| 10.0_f64.powf(n) * 10.0_f64.ln() - (9_u32.pow(5) as f64),
)
.unwrap()
.ceil() as u32;
let max_num = 9_u32.pow(5) * max_digits;
let mut result_sum: u32 = 0;
for n in 10..(max_num + 1) {
let mut temp_n = n;
let mut power_sum = 0;
while temp_n != 0 {
power_sum += (temp_n % 10).pow(5);
temp_n /= 10;
}
if power_sum == n {
result_sum += n;
}
}
result_sum.to_string()
}
}