# Answer to Question #14014 in C++ for king aliver mendiola

Question #14014

Goldbach's conjecture says that every positive even number greater than 2 is the sum of two prime numbers. Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case. It has been numerically confirmed up to very large numbers (much larger than we can go with our Prolog system). Write a predicate to find the two prime numbers that sum up to a given even integer.

Expert's answer

#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;

__int64 powInt(__int64 &a, int n)

{

__int64 res = 1;

while (n)

{

& if (n&1) res*=a;

& a*=a;

& n>>=1;

}

return res;

}

bool Ferma (const int n,const __int64 k = 20)//ôóíêö³ÿ âèêîíóºòüñÿ ïðàâèëüíî, ïîêè íå áóäå ïåðåïîâíåííÿ òèï³â

{

if (n%2)

{

& srand(time(0));

& __int64 a, b;

& for (int i=0 ; i<k ; ++i)

& {

& a=((rand()%(n-2))+1);

& b=(powInt(a,n-1))%n;

& if (b!=1)

& return 1;

& }

& return 0;

}

else return 1;

}

int main () {

cout<<"Input the number \n";

int num;

cin>>num;

int numDtwo = num / 2 - 1;

int numDone = num / 2 + 1;

for (int i = 0 ; i < num / 2 ; i++)

{

& if (Ferma(numDone) == 0 && Ferma(numDtwo) == 0) {

& cout << numDone << ' ' << numDtwo;

& break;

& }

& numDtwo--;

& numDone++;

}

system("pause");

return 0;

}

#include <stdlib.h>

#include <time.h>

using namespace std;

__int64 powInt(__int64 &a, int n)

{

__int64 res = 1;

while (n)

{

& if (n&1) res*=a;

& a*=a;

& n>>=1;

}

return res;

}

bool Ferma (const int n,const __int64 k = 20)//ôóíêö³ÿ âèêîíóºòüñÿ ïðàâèëüíî, ïîêè íå áóäå ïåðåïîâíåííÿ òèï³â

{

if (n%2)

{

& srand(time(0));

& __int64 a, b;

& for (int i=0 ; i<k ; ++i)

& {

& a=((rand()%(n-2))+1);

& b=(powInt(a,n-1))%n;

& if (b!=1)

& return 1;

& }

& return 0;

}

else return 1;

}

int main () {

cout<<"Input the number \n";

int num;

cin>>num;

int numDtwo = num / 2 - 1;

int numDone = num / 2 + 1;

for (int i = 0 ; i < num / 2 ; i++)

{

& if (Ferma(numDone) == 0 && Ferma(numDtwo) == 0) {

& cout << numDone << ' ' << numDtwo;

& break;

& }

& numDtwo--;

& numDone++;

}

system("pause");

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment