now I have never posted before so I hope this is in the right spot/format..

this problem is driving me insane. I need prove the Goldbach conjecture that is, "every even integer n > 2 is equal to the sum of 2 prime numbers." Then with a starting point and ending point prove this. (Ei: start at 2 end at 10 => 2 = 1 +1, 4 = 1 + 3....10 = 5 + 5)

The problem that I have is that I cant seem to figure out what is wrong with my code. When I run it the loop jumps over some intervals that it couldn't find a prime for and I just don't really know how to fix this as well as making the intervals go up 2 instead of 1 without a syntax error.

now this is what I have so far...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


/* function to create a random number */

int randomInteger(int low, int high)
{
	static short firstRun = 1;
	int offset;
	if(firstRun)
	{
		//If this is the first call to this procedure, randomize the seed
		srand(time(NULL));
		//and set first run to 0 so we know its  been run
		firstRun = 0;
	}
	offset = low;
	low = 0;
	high -=offset;
	return ((rand()%high)+offset);
}


/* function to see if a number is prime */

int prime_check(int num)
{
	int i;
	int check = 0;

	for(i=1;i<=num;i++)
	{
		if(num % i == 0)
		{
			check = check +1;
		}
	}

	if(check == 2)
	/* printf("number is prime\n"); */
	return 1;

	else
	/*printf("number is not prime\n");*/
	return 0;
}




int main()
{
	int a,b,i;
	int prime1, prime2;
	int start = 2;
	int end = 20;


	for(i = start; i <= end; i++)
	{
		prime1 = randomInteger(1,i);

		a = prime_check(prime1);

		if(a==1)
		{
			prime2 = (i - prime1);

			b = prime_check(prime2);

			if(b==1)
			printf("%d = %d + %d\n", i, prime1, prime2);

			else
			randomInteger(1,i);
		}

		else
		randomInteger(1,i);


		/*lets me see what numbers the program skipped*/

		printf("%d\n", i);


	}

	return 0;
}

when I compile and run it I get something that looks like this...

2
3
4 = 2 +2
5 = 3 +2
6
7 = 5 + 2
8 = 5 + 3
9
10 = 5 +5
11
12
13
14 = 7 +7
15
16
17
18 = 5 + 13
19 = 17 + 2
20

from a glance:
- for the Goldbach conjecture it's greater than 2 (1 is not a prime), so the loop should start at 4, the next even, right?
-also why not just do i+2 for the increment expression (instead of i++) for the for loop in main since it only applies to finding even numbers?
- with your main function it is possible to not find the primes since you could call randominteger() and the returned integer may not be prime (and/or the second random integer) and if the return from randominteger doesn't result in primes you don't continue to search for the primes after, but instead continue onto the next integer which you shouldn't until two primes are found that add up to the current even number and then iterate to the next even.
- in order to prove the theorem(through your program) you would have test every number indefinitely which is not possible, so as part of the theorem you would have to supply a range as well say n <= 20, and show those results exhaustively.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.