fork(3) download
  1. #include <iostream>
  2. #include <iterator>
  3. #include <algorithm>
  4.  
  5. template <typename T, int N>
  6. int size(T (&)[N])
  7. {
  8. return N;
  9. }
  10.  
  11. bool primeCheck(int p) {
  12. if(p<2) return false;
  13.  
  14. // Really slow way to check, but works
  15. for(int d = 2; d<p; ++d) {
  16. if (0==p%d) return false; // found a divisor
  17. }
  18. return true; // no divisors found
  19. }
  20.  
  21. int countPrimes(int *a, int size) {
  22. int numberPrime = 0;
  23. for (int i = 0; i < size; ++i) {
  24. // For each element in the input array, check it,
  25. // and increment the count if it is prime.
  26. if(primeCheck(a[i]))
  27. ++numberPrime;
  28. }
  29. return numberPrime;
  30. }
  31.  
  32. int main() {
  33. int input[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  34. // * * * * * 5 total
  35. std::cout << countPrimes(input, size(input)) << '\n';
  36. std::cout << std::count_if(std::begin(input), std::end(input), primeCheck) << '\n';
  37. return 0;
  38. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
5
5