C++ program to find primes Part C: Write a program to find, and provide a count
ID: 3891888 • Letter: C
Question
C++ program to find primes
Part C: Write a program to find, and provide a count of prime numbers between 1 and a user provided limit or maximum value. For example, between 1 and 10,000 there are exactly 1229 prime numbers. In order to improve performance, the program should use threads to perform computations in paralle Additionally, the program should use the C++11 high resolution clock to provide an execution time in milliseconds The program should read the thread count and limit from the command line in the following format: ./primes -t -l The way to determine if a number is prime is as follows: .The numbers 2 and 3 are prime For a given number, i >3: .If i is divisible by 2, then i is not prime .If i is not divisible by 2, then: Calculate k the approximate square root of i Try to divide i by all odd numbers 3, 5, 7, etc. less than or equal to k .If i is divisible by any of these numbers, theni is not prime To estimate the square root of a number, use the following algorithm: rnumber Stop iterating when the difference between the current and previous estimates isExplanation / Answer
#include<iostream>
using namespace std;
int main()
{
int count=0;
double sqrtEstCurr=0, number, sqrtEstPrev;
int limit;
cin >> limit;
if (limit > 0 && limit <= 2)
count = 1;
else if (limit <= 3)
count = 2;
else
{
for (int i = 4; i <= limit; i++) {
if (i % 2 != 0) {
int K = sqrt(i);
number = K;
sqrtEstPrev = sqrtEstCurr;
sqrtEstCurr = ((number / sqrtEstCurr) + sqrtEstCurr) / 2;
if (sqrtEstCurr - sqrtEstPrev < 1)
break;
for (int j = 3; j <= K; j = j + 2) {
if (i%j != 0)
count++;
}
}
}
}
cout << count << endl;
system("pause");
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.