I just started to code and try to build my first function in Python/NumPy. My first goal was to get a list for all multiplies of 31 which are not divisible by numbers 1>31 and I figured it out
Here is the code:
JavaScript
x
6
1
c = float(input("Range?"))
2
a = np.arange(1,c)
3
A = np.array([31*a])
4
B=(A[(A%2!=0) & (A%3!=0) & (A%5!=0) & (A%7!=0) & (A%11!=0) & (A%13!=0) & (A%17!=0) & (A%19!=0) & (A%23!=0) & (A%29!=0)] )
5
print(B)
6
The next step is to make the function more flexible, but easy ideas like this just don’t work, obviously:
JavaScript
1
6
1
c = float(input("Range?"))
2
a = np.arange(1,c)
3
d = float(input("multiplier of?"))
4
A = np.array([d*a])
5
C=(A[(A%d!=0)])
6
Advertisement
Answer
The answer will be something like
JavaScript
1
17
17
1
def can_multiply(border_num, num) -> bool:
2
# 2 instead of 1 here because every num is divisible to 1
3
arr = [x for x in range(2, border_num)]
4
for number in arr:
5
if num % number == 0:
6
return False
7
return True
8
9
10
c = float(input("Range?"))
11
a = np.arange(1, c)
12
d = float(input("multiplier of?"))
13
A = np.array([d * a])
14
print(A[0])
15
C = [x for x in A[0] if can_multiply(int(d), x)]
16
print(C)
17
where [x for x in A[0] if ....]
is a list comprehension meaning we take every element in array A[0] and add it to the new array C if can_multiply
func returns True