Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to get digit count of arbitrarily big number

What is the most efficient way to get the digits of a number?

Lets begin with an example:

Imagine the Fibonacci sequence. Now lets say we want to know which Fibonacci number is the first to have 1000 digits (in base 10 representation). Up to 308 digits (1476th Fibonacci number) we can easily do this by using logBase 10 <number>. If the number is greater than the 1476th Fibonacci number, logBase will return Infinity and the calculation will fail. The problem is that 308 is somewhat far away from 1000, which was our initial goal.

A possible solution is to convert the number we want to know the number of digits of to a string and use it's length to determine the digit count. This is a little bit inefficient for my purposes because trying this with 10000 takes its sweet time.

The most efficient method shown in other questions is hardcoding all possible cases which I really do not want to do, especially because the number of digits exceeds 10 as needed in the proposed solutions.

So to come back to my question: What is the best (most efficient) way to determine a base 10 numbers digit count? Is it really converting it to a string and using its length or are there any "hacker" tricks like 0x5f3759df?

Note: I appreciate solutions in any language, even if this is tagged "haskell".

like image 582
ThreeFx Avatar asked Jul 28 '14 23:07

ThreeFx


People also ask

How do you count the number of digits in a number?

The formula will be integer of (log10(number) + 1). For an example, if the number is 1245, then it is above 1000, and below 10000, so the log value will be in range 3 < log10(1245) < 4. Now taking the integer, it will be 3. Then add 1 with it to get number of digits.

How do you find the length of a digit in Python?

The function len() is one of Python's built-in functions. It returns the length of an object.


1 Answers

Why not use div until it's no longer greater than 10?

digitCount :: Integer -> Int
digitCount = go 1 . abs
    where
        go ds n = if n >= 10 then go (ds + 1) (n `div` 10) else ds

This is O(n) complexity, where n is the number of digits, and you could speed it up easily by checking against 1000, then 100, then 10, but this will probably be sufficient for most uses.


For reference, on my not-so-great laptop running it only in GHCi and using the horribly inaccurate :set +s statistics flag:

> let x = 10 ^ 10000 :: Integer
> :force x
<prints out 10 ^ 10000>
> digitCount x
10001
it :: Int
(0.06 secs, 23759220 bytes)

So it seems pretty quick, it can churn through a 10001 digit number in less than a 10th of a second without optimizations.


If you really wanted the O(log(n)) complexity, I would recommend writing your own version where you divide by 2 each time, but that one is a little more involved and trickier than dividing by 10. For your purposes this version will easily compute the number of digits up to about 20000 digits without problems.

like image 198
bheklilr Avatar answered Sep 28 '22 15:09

bheklilr