Question

I am missing here the technical word but the problem here is either to change int to float or float to 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

The err -- tried fixing it with functions such as int() and float() but still persist:

TypeError: 'float' object cannot be interpreted as an integer
Was it helpful?

Solution

This expression is your immediate problem:

float(factorize(n/candidate))

factorize returns a list, but float needs its argument to be a string or a number.

(Your code has many, many other problems but perhaps it would be best for you to discover them for yourself...)

OTHER TIPS

Notice that you return a list and in the line:

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

But float works on numbers or strings, not a list.

The correct solution would be:

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

Cannot understand what Gareth meant with many, many other problems, the problem is in sanitization!

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]

Better but can you do it simpler or fewer lines?

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 comparison

$ 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

The at least O(n) is a big understatement, lol what I can find from google, let's consider the poor result with large prime. 10003 pawns at least 10002! subprocesses, 10003 pawns 10002 because each fail and it cannot be evaluated until each of their subprocesses are evaluated and each n subprocess will have n-1 subprocesses. Good example how not to factorize.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top