Ad
Algorithms
Code
Diff
  • fn digits(mut n: u64) -> usize {
        std::iter::from_fn(|| { n /= 10; (n != 0).then_some(n) }).count() + 1
    }
    • use std::collections::BTreeMap;
    • fn digits(n: u64) -> usize {
    • let powers = (0..20).map(|n| (
    • if n == 0 { 0 } else { 10_u64.pow(n) },
    • n as usize + 1
    • )).collect::<BTreeMap<_, _>>();
    • return *powers.range(..=n).last().unwrap().1;
    • fn digits(mut n: u64) -> usize {
    • std::iter::from_fn(|| { n /= 10; (n != 0).then_some(n) }).count() + 1
    • }