How to count the number of digits in numbers in different bases?

2024/11/15 21:50:35

I'm working with numbers in different bases (base-10, base-8, base-16, etc). I'm trying to count the number of characters in each number.

Example

Number: ABCDEF

Number of digits: 6

I know about the method based on logarithms but I'm facing some problems.

  1. This Python script outputs that it failed to calculate the number of digits correctly in 3,969 numbers out of 1,000,000.

  2. I think the method that uses logarithms could be rather slow

Links:

  • This C program must be very slow (what if I have a very great number?). It also can't deal with numbers in different bases (for example, base-16).

  • Not a dupe of this as there the OP was asking only about base-10


Edit: certainly I can calculate the length of a string but what interests me most, is if it is possible to do the calculation without convention to string. I'd want to know the algorithm that could help to do it knowing just the source-base and the base to convert to.

Edit2: source-base is base-10 and the base to convert to can be any other base.


How can we calculate the number of digits in numbers in different bases?

If I know the number in base-10, how do I calculate the number of digits in the same number converted to base-16 (base-8, etc) without performing the conversion?

Note: some Python or C code will be greatly appreciated

Answer

Logarithms shouldn't really be slow. And you can easily calculate logarithms to any base by this formula: logBaseN(x)=logBaseA(x)/logBaseA(N) - you can use ln(Base e = 2.718...) or logBase10 or whatever you have. So you don't really need a program, a formular should do it:

num_digets(N, base) = 1 + floor(log(N) / log(base))

where N is your number and base the base you want that number in.

For more explanation take a look here: http://www.mathpath.org/concepts/Num/numdigits.htm

https://en.xdnf.cn/q/71417.html

Related Q&A

Pandas KeyError using pivot

Im new to Python and I would like to use Python to replicate a common excel task. If such a question has already been answered, please let me know. Ive been unable to find it. I have the following p…

Not found: Container localhost does not exist when I load model with tensorflow and flask

I am a newbie research Deeplearning. I load a saved model with tensorflow and made a API with flask but I get error Container localhost does not exist. when I predict, please help me fix it. Thank you.…

Python: simplifying nested FOR loop?

I am wondering if there is a way to simplify the nested loop below. The difficulty is that the iterator for each loop depends on things from the previous loops. Here is the code:# Find the number of co…

NLTK Data installation issues

I am trying to install NLTK Data on Mac OSX 10.9 . The download directory to be set, as mentioned in NLTK 3.0 documentation, is /usr/share/nltk_data for central installation. But for this path, I get …

Why does the simplest requests_mock example fail with pytest?

I have a peculiar problem with requests_mock. I want to use it with pytest to test my API wrapper library.Ive tried to use the first example in the requests_mock docs, except I put it in a test_mock()-…

Error installing PyCurl

I tried installing pycurl via pip. it didnt work and instead it gives me this error.running installrunning buildrunning build_pyrunning build_extbuilding pycurl extensiongcc-4.2 -fno-strict-aliasing -f…

Serializing objects containing django querysets

Django provides tools to serialize querysets (django.core.serializers), but what about serializing querysets living inside other objects (like dictionaries)?I want to serialize the following dictionar…

How do I list my scheduled queries via the Python google client API?

I have set up my service account and I can run queries on bigQuery using client.query(). I could just write all my scheduled queries into this new client.query() format but I already have many schedule…

What does conda env do under the hood?

After searching and not finding, I must ask here:How does conda env work under the hood, meaning, how does anaconda handle environments?To clarify, I would like an answer or a reference to questions l…

Numpy array larger than RAM: write to disk or out-of-core solution?

I have the following workflow, whereby I append data to an empty pandas Series object. (This empty array could also be a NumPy array, or even a basic list.)in_memory_array = pd.Series([])for df in list…