【问题标题】:Explain the float - int problem in factorization解释因式分解中的 float - int 问题
【发布时间】:2011-01-19 16:44:28
【问题描述】:

我在这里遗漏了技术词,但这里的问题是将 int 更改为 float 或将 float 更改为 int。

def factorize(n):
    def isPrime(n):
        return not [x for x in range(2,int(math.sqrt(n)))
                    if n%x == 0]
    primes = []
    candidates = range(2,n+1)
    candidate = 2
    while not primes and candidate in candidates:
        if n%candidate == 0 and isPrime(candidate):

            # WHY ERROR?
            #I have tried here to add float(), int() but cannot understand why it returns err
            primes = primes + [float(candidate)] + float(factorize(n/candidate))
        candidate += 1
    return primes

错误 - 尝试使用 int()float() 等函数修复它,但仍然存在:

TypeError: 'float' object cannot be interpreted as an integer

【问题讨论】:

  • factorize 函数中为什么要包含浮点数?
  • dan04:只是过度使用,杀了它。现在它可以工作了,但仍然想知道其他问题,更简洁?

标签: python floating-point int factorization


【解决方案1】:

这个表达式是你的直接问题:

float(factorize(n/candidate))

factorize 返回一个列表,但float 需要它的参数是字符串或数字。

(您的代码有很多很多其他问题,但也许最好让您自己发现它们......)

【讨论】:

    【解决方案2】:

    请注意,您返回 list 并在该行中:

    primes = primes + [float(candidate)] + float(factorize(n/candidate))
    

    但是float 作用于数字或字符串,而不是列表。

    正确的解决方案是:

    primes = primes + [float(candidate)] + [float(x) for x in factorize(n/candidate)]
    # Converting every element to a float
    

    【讨论】:

      【解决方案3】:

      无法理解 Gareth 对 many, many other problems 的含义,问题在于消毒!

      def factorize(n):
          # now I won`t get floats
          n=int(n)
      
          def isPrime(n):
              return not [x for x in range(2,int(math.sqrt(n)))
                          if n%x == 0]
      
          primes = []
          candidates = range(2,n+1)
          candidate = 2
          while not primes and candidate in candidates:
              if n%candidate == 0 and isPrime(candidate):
                  primes = primes + [candidate] + factorize(n/candidate)
              candidate += 1
          return primes
      
      
      clearString = sys.argv[1]
      obfuscated = 34532.334
      factorized = factorize(obfuscated)
      
      print("#OUTPUT "+factorized)
      
      
      #OUTPUT [2, 2, 89, 97]
      

      更好,但你能做到更简单或更少行吗?

      def factorize(n):
          """ returns factors to n """
      
          while(1):
                  if n == 1:
                          break
      
                  c = 2 
      
                  while n % c != 0:
                          c +=1
      
                  yield c
                  n /= c
      
       print([x for x in factorize(10003)])
      

      时间比较

      $ time python3.1 sieve.py 
      [100003]
      
      real    0m0.086s
      user    0m0.080s
      sys 0m0.008s
      $ time python3.1 bad.py 
      ^CTraceback (most recent call last):
        File "obfuscate128.py", line 25, in <module>
          print(factorize(1000003))
        File "obfuscate128.py", line 19, in factorize
          if n%candidate == 0 and isPrime(candidate):
      KeyboardInterrupt
      
      real    8m24.323s
      user    8m24.320s
      sys 0m0.016s
      

      at least O(n) 是一个很大的轻描淡写,哈哈,我可以从谷歌找到,让我们考虑一下大素数的糟糕结果。 10003 至少典当 10002! 子进程,10003 典当 10002 因为每个失败并且在评估其每个子流程之前无法评估它,并且每个 n 子流程将具有 n-1 子流程。如何不分解的好例子。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 2014-01-12
        • 2019-10-23
        相关资源
        最近更新 更多