mykatas/rust/tests/1_persistent_bugger.rs

24 lines
559 B
Rust

fn persistence(num: u64) -> u64 {
match num {
0..=9 => 0,
_ => {
let res = num
.to_string()
.chars()
.map(|c| c.to_digit(10).unwrap() as u64)
.product();
1 + persistence(res)
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn sample_tests() {
assert_eq!(super::persistence(39), 3);
assert_eq!(super::persistence(4), 0);
assert_eq!(super::persistence(25), 2);
assert_eq!(super::persistence(999), 4);
}
}