Thread: number of digits in number?

  1. #16
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    That's a cool little algorithm Salem. Kind of like binary search. Although it would be harder to extend to 64 bits, I'd bet it it would extend it's performance advantage. O(lg(lg(n)) performance, rather than O(lg(n)) for the divBy10 (and likely the same for the log, but one doesn't really know how it's implemented).
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Since division is always going to be more expensive than comparison, you can do quite a lot of comparing in the time it takes to do one division.

    For 64 bit integers, I might use Nick's approach, but divide by 1000000 not 1000, and follow it with my if/else code suitably trimmed for the reduced number range.
    For a 64 bit number, there is at most 3 iterations of divide by 1000000, which isn't too bad.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  2. Learning Memory, Ins and Outs?
    By Zoiked in forum C Programming
    Replies: 1
    Last Post: 08-27-2007, 04:43 PM
  3. numeric header - number of digits
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2006, 12:02 PM
  4. Count the number of vowels, consonants, digits etc.
    By kumar14878 in forum C Programming
    Replies: 3
    Last Post: 05-09-2005, 12:34 AM
  5. Extracting individual digits from a short
    By G'n'R in forum C Programming
    Replies: 9
    Last Post: 08-30-2001, 10:30 AM