Most efficient way to get digit count of arbitrarily big number

ThreeFx

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".

bheklilr

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Most efficient way to look for the last digit of a number?

From Dev

Most efficient way to find the last digit of a^b

From Dev

Most efficient way to count occurrences?

From Dev

Most efficient way to get the highest number from a collection of integers

From Dev

Efficient way to count the digits of very big factorials

From Dev

Efficient way to count the digits of very big factorials

From Dev

Most efficient way to count number of pairs in an array whose product is a perfect square?

From Dev

Most efficient way to check number of CGPoints in CGPathRef

From Dev

Most efficient way to add individual digits of a number

From Java

Most efficient way to get the last element of a stream

From Dev

The most efficient way to get algorithm complexity table

From Dev

Most efficient way to get seconds left in this hour

From Dev

Efficient way to count the number of elements of a kind in an array

From Dev

Idiomatic way to get every digit of a number

From Dev

Most efficient way to count occurrences in XQuery for multiple values

From Dev

spark scala most efficient way to do partial string count

From Dev

What is the most efficient way to count the intersections between two sets (Java)?

From Dev

ActiveRecord/Rails: most efficient way to count results of multiple queries

From Dev

Most efficient way to count occurrences in XQuery for multiple values

From Dev

Most efficient way to return a fixed number of values in SQL

From Dev

Most efficient way of splitting a number into whole and decimal parts

From Dev

Most efficient way to return a fixed number of values in SQL

From Dev

Is this most efficient way to find lowest number using function in C++?

From Dev

Most efficient way to check if numbers in a list is divisible by another number

From Dev

Most efficient way to run a check against a large number of words

From Dev

Most efficient way to implement this?

From Dev

Most efficient way to execute this

From Dev

Gdb Python API - Most efficient way to get the value of PC?

From Dev

What is the most efficient way to get the last line break in a string

Related Related

  1. 1

    Most efficient way to look for the last digit of a number?

  2. 2

    Most efficient way to find the last digit of a^b

  3. 3

    Most efficient way to count occurrences?

  4. 4

    Most efficient way to get the highest number from a collection of integers

  5. 5

    Efficient way to count the digits of very big factorials

  6. 6

    Efficient way to count the digits of very big factorials

  7. 7

    Most efficient way to count number of pairs in an array whose product is a perfect square?

  8. 8

    Most efficient way to check number of CGPoints in CGPathRef

  9. 9

    Most efficient way to add individual digits of a number

  10. 10

    Most efficient way to get the last element of a stream

  11. 11

    The most efficient way to get algorithm complexity table

  12. 12

    Most efficient way to get seconds left in this hour

  13. 13

    Efficient way to count the number of elements of a kind in an array

  14. 14

    Idiomatic way to get every digit of a number

  15. 15

    Most efficient way to count occurrences in XQuery for multiple values

  16. 16

    spark scala most efficient way to do partial string count

  17. 17

    What is the most efficient way to count the intersections between two sets (Java)?

  18. 18

    ActiveRecord/Rails: most efficient way to count results of multiple queries

  19. 19

    Most efficient way to count occurrences in XQuery for multiple values

  20. 20

    Most efficient way to return a fixed number of values in SQL

  21. 21

    Most efficient way of splitting a number into whole and decimal parts

  22. 22

    Most efficient way to return a fixed number of values in SQL

  23. 23

    Is this most efficient way to find lowest number using function in C++?

  24. 24

    Most efficient way to check if numbers in a list is divisible by another number

  25. 25

    Most efficient way to run a check against a large number of words

  26. 26

    Most efficient way to implement this?

  27. 27

    Most efficient way to execute this

  28. 28

    Gdb Python API - Most efficient way to get the value of PC?

  29. 29

    What is the most efficient way to get the last line break in a string

HotTag

Archive