exercism-solutions/rust/armstrong-numbers/src/lib.rs

14 lines
397 B
Rust
Raw Normal View History

2024-10-24 13:03:37 -05:00
pub fn is_armstrong_number(num: u32) -> bool {
let Some(count) = num.checked_ilog10().map(|count| count + 1) else {
return true;
};
let armstrong_sum = (0..count).map(|i| {
let divisor = 10u32.pow(i);
let quotient = num / divisor;
let digit = quotient % 10;
let raised = digit.pow(count);
raised
}).sum();
num == armstrong_sum
}