mimus
mimus

Reputation: 367

how to change this code that use xrange to run in python 3?

I'm reading the High-Performance Python book from O'Reilly collection, in the page number 11 I found this code that works for python 2, the point here is to make one instruction that performs(by vectorizing) several at the same time

import math 
def check_prime(number):
    sqrt_number = math.sqrt(number)
    number_float = float(number)
    numbers = range(2, int(sqrt_number)+1)
    for i in xrange(0, len(numbers), 5):
          # the following line is not valid Python code
          result = (number_float / numbers[i:(i+5)]).is_integer()
          if any(result):
                 return False
    return True

but I get this error

TypeError: unsupported operand type(s) for /: 'float' and 'list'

I've tried to change it to work on python 3 here is my try:

import math

def check_prime(number):
    sqrt_number = math.sqrt(number)
    number_float = float(number)
    numbers = list(range(2, int(sqrt_number)+1))

    for i in range(0, len(numbers), 5):
        # the following line is not valid Python code
        result = (number_float / numbers[i:(i+5)]).is_integer()
        if any(result):
            return False
    return True

I changed the xrange for range and the range(2, int(sqrt_number)+1) for list(range(2, int(sqrt_number)+1)) but i did not have succeed in this. I suppose there is a special operator for sets or something like that but have no idea. if any of you people can help me I'll be so grateful whit you

Upvotes: 2

Views: 160

Answers (2)

CycleOfTheAbsurd
CycleOfTheAbsurd

Reputation: 178

I looked at the book and that line is not supposed to actually work as is; you cannot divide by a list in Python. The author uses that code as an example of what vectorization would look like. The # the following line is not valid Python code comment is in the original to indicate that.

The closest in term of functionality and semantics would probably be this code:

import math

def check_prime(number):
    sqrt_number = math.sqrt(number)
    number_float = float(number)
    numbers = list(range(2, int(sqrt_number)+1))

    for i in range(0, len(numbers), 5):
        # the following line is now valid Python code, but not vectorized
        result = [(number_float / n).is_integer for n in numbers[i:(i+5)]]
        if any(result):
            return False
    return True

Note that the processing for result in this version is not done in parallel, so it's probably not what the author wanted to demonstrate. As far as I know, vectorization isn't natively available in Python, you would have to use numpy to do it. This article should be useful if you want to try it.

Upvotes: 2

deadshot
deadshot

Reputation: 9061

Try this:

import math

def check_prime(number):
    sqrt_number = math.sqrt(number)
    number_float = float(number)
    numbers = list(range(2, int(sqrt_number)+1))

    for i in range(0, len(numbers), 5):
        result = [number_float % num == 0 for num in numbers[i:(i+5)]]
        if any(result):
            return False
    return True

Upvotes: 2

Related Questions