Python 解释因子分解中的浮点整型问题

Python 解释因子分解中的浮点整型问题,python,floating-point,int,factorization,Python,Floating Point,Int,Factorization,我这里缺少了技术术语,但这里的问题是要么将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 n

我这里缺少了技术术语,但这里的问题是要么将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

此表达式是您的直接问题:

float(factorize(n/candidate))
factorize
返回一个列表,但
float
需要其参数为字符串或数字


(您的代码还有很多其他问题,但您最好自己去发现它们…

请注意,您返回了一个
列表,并在以下行中:

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

无法理解Gareth对
许多其他问题的意思,问题在于消毒

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
$time python3.1 sieve.py
[100003]
实0.086s
用户0m0.080s
sys 0m0.008s
$time python3.1 bad.py
^CTraceback(最近一次通话最后一次):
文件“obfuscate128.py”,第25行,在
打印(因子分解(1000003))
文件“obfuscate128.py”,第19行,在factorize中
如果n%candidate==0且iPrime(候选者):
键盘中断
real 8m24.323s
用户8m24.320s
系统0m0.016s

<> >代码>至少O(n)< /代码>是一个很大的轻描淡写,LOL我可以从谷歌中找到,让我们考虑大Prime的差结果。代码>10003
至少典当
10002子流程,
10003
典当了
10002
,因为每个子流程都失败了,在每个
n
子流程都有
n-1
子流程之前,无法对其进行评估。如何不进行因式分解是一个很好的例子。

为什么在
因式分解
函数中会涉及浮点值?dan04:只是过度使用,导致它失效。现在它可以工作了,但仍然想知道其他问题,更简洁?