Helper Lambdas
Completed
Inside main, define a lambda isPrime and use it to print how many of the input numbers are prime and their sum.
Input
Line 1: n. Line 2: n integers.
Output
Two lines: count, sum.
Example
Input
8 1 2 3 4 5 9 11 15
Output
4 21
Editor
C++
Hints
auto f = [](int x) { ... }; defines a lambda you can call like a function.
Solution explanation
Lambdas are handy for small helpers used in only one place.
Show solution
#include <iostream>
using namespace std;
int main() {
auto isPrime = [](int x) {
if (x < 2) return false;
for (int d = 2; d * d <= x; d++) {
if (x % d == 0) return false;
}
return true;
};
int n, count = 0;
long long sum = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (isPrime(x)) {
count++;
sum += x;
}
}
cout << count << "\n" << sum << "\n";
return 0;
}