Sum of Primes in an Array
easySave
ArrayMathNumber Theory
Given an array of positive integers, your task is to calculate the sum of all the prime numbers present in the array. If there are no prime numbers, return 0.
Example 1
Input
[ 1, 2, 3, 4, 5, 6 ]
Output
10
Explanation. The primes in the array are 2, 3, and 5. Their sum is 2 + 3 + 5 = 10.
Example 2
Input
[ 10, 22, 33, 44, 55 ]
Output
0
Explanation. There are no prime numbers in the array. Thus, the sum is 0.
Example 3
Input
[ 17, 11, 20, 34, 23 ]
Output
51
Explanation. The prime numbers are 17, 11, and 23. The sum is 17 + 11 + 23 = 51.
Follow-up: How would you optimize the solution for a very large array?
Constraints:
The array will contain at least one element. All elements in the array will be positive integers.
- Views
- 3