mykatas/rust/tests/1_persistent_bugger.rs

25 lines
559 B
Rust
Raw Normal View History

2021-10-29 00:05:01 +03:00
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);
}
}