Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算对数,为什么这个算法效率不高,如何使它更有效?_Python_Algorithm_Logarithm - Fatal编程技术网

Python 计算对数,为什么这个算法效率不高,如何使它更有效?

Python 计算对数,为什么这个算法效率不高,如何使它更有效?,python,algorithm,logarithm,Python,Algorithm,Logarithm,我想知道我是否可以根据一个数字相对于一个基数(例如,以16为基数的对数2)来计算一个数字的对数,而不必实际使用log()。我设法做到了,但我认为这不是很有效 这是我用Python编写的代码: def myLog(x,b): exp=0 ans=b**exp while x!=ans: ans=b**exp if ans==x: return exp exp=exp+1 所以我可以给它myLog(16

我想知道我是否可以根据一个数字相对于一个基数(例如,以16为基数的对数2)来计算一个数字的对数,而不必实际使用
log()
。我设法做到了,但我认为这不是很有效

这是我用Python编写的代码:

def myLog(x,b):
    exp=0
    ans=b**exp
    while x!=ans:
        ans=b**exp
        if ans==x:
            return exp
        exp=exp+1
所以我可以给它
myLog(16,2)
,它应该返回4。确实如此,但我认为这不是最有效的方法,因此我如何修复它并使我的代码更有效,不仅在这种情况下,而且在大多数情况下?

因为它是一种:

另见:


    • 这是我的两分钱:

      def myLog(x,b):
          exp = 0
          ans = 1
          while ans<x:
              ans *= b
              exp += 1
          if ans == x:
              return exp  
          else:
              raise ValueError("can't find a suitable exponent")
      
      In [10]: myLog(16,2)
      Out[10]: 4
      
      def myLog(x,b):
      exp=0
      ans=1
      当ans尝试递归时:

      def func(a, b, ans=0):
          if a/b == 1:
              return ans + 1
          else: return func(a/b, b, ans+1)
      
      In [26]: func(16, 2)
      Out[26]: 4
      
      In [27]: func(8, 2)
      Out[27]: 3
      
      In [28]: func(16,4)
      Out[28]: 2
      

      如果有人给出一个负值,比如myLog(-1,2),或者如果它是1 myLog(1,2),那么你在循环之前计算ans,你知道它总是0,因为你把exp=0,然后在循环中,你在不改变exp的情况下再次计算它


      此版本增加了对非整数输出的支持:

      def log(a, b):
          b = float(b)
          a = float(a)
          g = a
          n = 0
          i = 1
          while b**i != 1:
              while g >= b**i:
                  g /= b**i
                  n += i
              i /= b
          return n
      
      不适用于所有数字。日志(5,10)在应为0.69897时返回0.00000假定:

      x: a positive integer
      b: a positive integer; b >= 2
      returns: log_b(x), or, the logarithm of x relative to a base b.
      
      似乎最短的路是:

      def myLog(x, b):
          ans = 0
          while b <= x:
              ans += 1
              x /= b
          return ans
      

      这几乎完全适合codereview.SE;]
      max(None,0)
      比这里的三元数更干净,或者
      ans=ans或0
      @AshwiniChaudhary:事实上,现在我想起来了,根本没有理由使用
      None
      默认值。只需将默认值设为0,以后不要再处理它。如果a/b==1:
      不能简化为
      如果a==b:
      ?hmn。。。我对它进行了测试,但出于某些原因,它似乎不起作用。注意:只有当
      a>=1
      b>0
      x: a positive integer
      b: a positive integer; b >= 2
      returns: log_b(x), or, the logarithm of x relative to a base b.
      
      def myLog(x, b):
          ans = 0
          while b <= x:
              ans += 1
              x /= b
          return ans
      
      def myLog(x, b):
          if (b > x): return 0
          else: return 1 + myLog(x/b, b)