fork download
  1. #include <iostream>
  2. #include <omp.h>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. // Function to check if a number is prime
  7. bool is_prime(int n) {
  8. if (n < 2) return false;
  9. for (int i = 2; i <= sqrt(n); ++i) {
  10. if (n % i == 0) return false;
  11. }
  12. return true;
  13. }
  14.  
  15. int main() {
  16. int n = 100; // Check first 100 odd integers
  17. int count = 0;
  18.  
  19. #pragma omp parallel for reduction(+:count)
  20. for (int i = 3; i < n; i += 2) {
  21. if (is_prime(i) && is_prime(i + 2)) {
  22. count++;
  23. }
  24. }
  25.  
  26. cout << "Number of prime pairs: " << count << endl;
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Number of prime pairs: 8