Recall that the following boolean function primecheck (int n) returns true or fa
ID: 3831691 • Letter: R
Question
Recall that the following boolean function primecheck (int n) returns true or false depending on whether or not the integer n (which is assumed to be at least 2) is prime. bool primecheck (int n) { int k = 2; while (k sqrt(n); } A very famous open question of number theory, called Goldbachs conjecture, is whether or not every even number 6 or greater is the sum of two positive odd primes. (For example: 6 is the sum of 3 and 3; 8 is the sum of 3 and 5; 10 is the sum of 3 and 7 and also the sum of 5 and 5, etc.) Write a C++ program which uses the above primecheck function to test Goldbachs conjecture. Specifically, your program should test every even number n between 6 and 10,000 inclusive to see whether or not n can be written as the sum of two positive odd primes. Your output should look something like: 6=3+3 8=3+5 10=3+7 12=5+7 14=3+11 16=3+13 18=5+13 . . . 10000=59+9941Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
bool primeCheck(int n)
{
int k = 2;
while(k <= sqrt(n) && n %k != 0)
k++;
return k > sqrt(n);
}
void checkGoldbach(int n)
{
if( n%2 != 0) n++;
for(int i = 6; i <= n; i += 2)
{
for(int j = 3; j<= n/2; j += 2)
{
if(primeCheck(j) && primeCheck(i-j))
{
cout << i << "="<<j<<"+"<<(i-j)<<endl;
break;
}
}
}
}
int main()
{
checkGoldbach(10000);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.