2

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks srry for the non-reading-bias i will be more careful next
Don't worry too much, it happens. And we're biased to expect code in a programming book to be valid
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.