I'm coding a function that tells which numbers are prime numbers in a given array. I would also like to do it without a for loop if it is possible
The numpy.divide function and division symbole (/) do not work as they throw a ValueError because the two arrays do not have the same shape
numbers = np.array([2, 3, 6])
def prime(numbers):
biggest = np.max(numbers)
division_array = np.arange(2, ceil(biggest / 2) + 1)
# division_array = [2, 3]
return numbers / division_array
print(numbers)
Expected output:
[[1, 0.6],
[1.5, 1],
[3, 2]]
Output:
ValueError: operands could not be broadcast together with shapes (4,) (3,)
numbersto use broadcasting:return numbers[:, np.newaxis] / division_arraynp.divide.outer([2, 3, 6], [2, 3])