Ad
Numbers
Data Types
Integers
Algorithms
Logic

Translation of henninglive's unrolled divison loop for calculating amount of digits to C#

Code
Diff
  • public class Kumite
    {
    // Unrolled div loop
      public static int Digits(ulong n)
      {
        var l = 1;
        while(true)
        {
          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;
        }
      }
    }
    • public class Kumite
    • {
    • // Unrolled div loop
    • fn digits(mut n: u64) -> usize {
    • let mut l = 1;
    • loop {
    • if n < 10 {
    • return l;
    • public static int Digits(ulong n)
    • {
    • var l = 1;
    • while(true)
    • {
    • 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;
    • }
    • if n < 100 {
    • return l + 1;
    • }
    • if n < 1000 {
    • return l + 2;
    • }
    • if n < 10000 {
    • return l + 3;
    • }
    • n /= 10000;
    • l += 4;
    • }
    • }