Goldbach's conjecture is one of the oldest and best-known unsolved problems in number theory and in all of mathematics. It states:
Every even integer greater than 2 can be expressed as the sum of two primes.
The conjecture has been shown to hold up through 4 × 1018 and is generally assumed to be true, but remains unproven despite considerable effort.
I make the project in C to prove his conjecture but I have missed a little detail. The program prints all the possible numbers that can create an N number.
Eg for number 14 the ouput prints
3 11
7 7
where the first number called
p1
and the second one is
p2
in my code.
I just want to keep the number with the smallest
p1
not all the numbers.
I though to store
p1
and
p2
to a 2d array but that's how I created the program and I don't want to start over. How can I fix it?
The code is the following:
#include<stdio.h>
#include<math.h>
int primecheck(long int x);
int main()
{
long int a,p1,p2;
int i,j,k;
FILE *input = fopen("goldbach.in","r");
fscanf(input,"%ld",&a);
fclose(input);
FILE *output = fopen("goldbach.out","w+");
if ((a%2)==1)
{
return 1;
}
for (p1=2;p1<a;++p1)
{
for (p2=2;p2<a;++p2)
{
j=primecheck(p1),k=primecheck(p2);
if ((j==0) && (k==0) && (p1+p2==a) && (p1<=p2))
fprintf(output,"%ld %ld\n",p1,p2);
}
}
fclose(output);
}
int primecheck(long int x)
{
long int i=2;
while (i<x)
{
if ((x%i)==0)
break;
++i;
}
if (i==x)
return(0);
else
return(1);
}