Ad
Numbers
Data Types
Integers
Algorithms
Logic

match point

Code
Diff
  • // Removed loop
    fn digits (n: u64) -> usize
    {
      match n {
        0 ..= 9 => 1,
        10 ..= 99 => 2,
        100 ..= 999 => 3,
        1000 ..= 9999 => 4,
        10000 ..= 99999 => 5,
        100000 ..= 999999 => 6,
        1000000 ..= 9999999 => 7,
        10000000 ..= 99999999 => 8,
        100000000 ..= 999999999 => 9,
        1000000000 ..= 9999999999 => 10,
        10000000000 ..= 99999999999 => 11,
        100000000000 ..= 999999999999 => 12,
        1000000000000 ..= 9999999999999 => 13,
        10000000000000 ..= 99999999999999 => 14,
        100000000000000 ..= 999999999999999 => 15,
        1000000000000000 ..= 9999999999999999 => 16,
        10000000000000000 ..= 99999999999999999 => 17,
        100000000000000000 ..= 999999999999999999 => 18,
        1000000000000000000 ..= 9999999999999999999 => 19,
        _ => 20,
      }
    }
    
    • // Unrolled div loop
    • fn digits(mut n: u64) -> usize {
    • let mut l = 1;
    • loop {
    • if n < 10 {
    • return l;
    • }
    • if n < 100 {
    • return l + 1;
    • }
    • if n < 1000 {
    • return l + 2;
    • }
    • if n < 10000 {
    • return l + 3;
    • }
    • n /= 10000;
    • l += 4;
    • // Removed loop
    • fn digits (n: u64) -> usize
    • {
    • match n {
    • 0 ..= 9 => 1,
    • 10 ..= 99 => 2,
    • 100 ..= 999 => 3,
    • 1000 ..= 9999 => 4,
    • 10000 ..= 99999 => 5,
    • 100000 ..= 999999 => 6,
    • 1000000 ..= 9999999 => 7,
    • 10000000 ..= 99999999 => 8,
    • 100000000 ..= 999999999 => 9,
    • 1000000000 ..= 9999999999 => 10,
    • 10000000000 ..= 99999999999 => 11,
    • 100000000000 ..= 999999999999 => 12,
    • 1000000000000 ..= 9999999999999 => 13,
    • 10000000000000 ..= 99999999999999 => 14,
    • 100000000000000 ..= 999999999999999 => 15,
    • 1000000000000000 ..= 9999999999999999 => 16,
    • 10000000000000000 ..= 99999999999999999 => 17,
    • 100000000000000000 ..= 999999999999999999 => 18,
    • 1000000000000000000 ..= 9999999999999999999 => 19,
    • _ => 20,
    • }