如何计算不同基数的数字位数? [英] How to count the number of digits in numbers in different bases?

查看:136
本文介绍了如何计算不同基数的数字位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用不同基数(base-10,base-8,base-16等)的数字。我正在尝试计算每个数字中的字符数。

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.

示例


数字: ABCDEF

位数: 6

我了解基于对数的方法,但是遇到了一些问题。

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


  1. 此Python脚本输出的结果是,它无法正确计算1,000,000个数字中的3,969个数字的位数。

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

我认为使用对数的方法可能会比较慢

I think the method that uses logarithms could be rather slow

链接:


  • 此C程序必须非常慢(如果我的数字很大,该怎么办?)。它也不能处理不同基数的数字(例如,基数为16)。

  • 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).

不是,因为那里OP仅询问以10为底的问题

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

编辑:当然,我可以计算字符串的长度但最让我感兴趣的是,是否有可能进行计算而无需约定。我想知道仅了解源库要转换为的库的算法,就可以帮助实现这一目标。

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 源库 base-10 要转换为的基数可以是任何其他基数。

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?

如果我知道以10为底的数字,我该如何计算将相同数字转换为以16为底的数字(以8为底的数字)不执行转换

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?

注意:一些Python或C代码将不胜感激

Note: some Python or C code will be greatly appreciated

推荐答案

对数并不应该太慢。您可以通过以下公式轻松计算对数为任何底数: logBaseN(x)= logBaseA(x)/ logBaseA(N)-您可以使用 ln (基本e = 2.718 ...)或 logBase10 或您拥有的任何东西。因此,您实际上并不需要程序,公式编写者应该这样做:

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))

其中 N 是您的电话号码,个基数是您要输入该数字的基数。

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

更多信息请参考在这里查看:
http://www.mathpath.org/concepts/ Num / numdigits.htm

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

这篇关于如何计算不同基数的数字位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆