Here is a way to print the sum of the prime numbers. The simplified logic helps you with your interviews and projects.

Prime number logic divides the number by 2; if the remainder is zero, you can say it is a prime number. The same is applied in this program.
The sum of prime numbers
import numpy as np
def is_prime(num):
if num < 2:
return False
for i in range(2, int(np.sqrt(num)) + 1):
if num % i == 0:
return False
return True
arr1 = np.array([5,10,17,23,30,47,50])
prime_list = np.array([])
prime_sum = 0
for num in arr1:
if is_prime(num):
prime_list = np.append(prime_list, num)
prime_sum += num
print("prime list:", prime_list)
print("prime sum:", prime_sum)
Advertisements
Here’s the output, which prints the prime number list and the sum of these.
prime list: [ 5. 17. 23. 47.]
prime sum: 92
References
A helpful book you need for your practice is Python Data Structures.
Related







You must be logged in to post a comment.