generate functionTue, 25 Jul 2023

Develop a C++ program to find all prime numbers within a given range using parallel processing, making use of multiple cores to speed up the computation. The program should take the start and end integers of the range as input and display all prime numbers within that range.

#include <iostream> #include <thread> using namespace std; void checkPrime(int num){ bool isPrime = true; int sqrtNum = (int) sqrt(num); for(int i = 2; i <= sqrtNum; i++){ if(num % i == 0){ isPrime = false; break; } } if(isPrime){ cout << num << " is a prime number" << endl; }else{ cout << num << " is not a prime number" << endl; } } int main(){ int start, end; cout << "Input start and end: "; cin >> start >> end; thread t1(checkPrime, start); thread t2(checkPrime, end); t1.join(); t2.join(); return 0; }

Questions about programming?Chat with your personal AI assistant