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