Okay, I have the program more or less written but the output shows nothing but zeros for the two prime numbers being added.
i.e.
4 = 0 + 0
6 = 0 + 0
etc..
Here is my code:
Code:
// This program demonstrates Goldbach's Conjecture
// that every even number greater than 2 is the sum
// of two prime numbers. The user is asked to input
// an upper and lower bound and the program shows each even number
// and shows two prime numbers that sum up to the even number.
#include <stdio.h>
int i, num, n, m, a, b, k, x, j, number;
int goldbach(i, a, b)
{
// Since the highest number we need to check for prime is half the total value
// we only need to check up to that number
for (x = 2; x <= (i / 2); ++x)
{
if(prime(x) && prime(i - x))
{
a = x;
b = i - x;
return 1;
break;
}
}
}
int prime(j)
{
int result = 1, k;
if(j == 1 || j == 2)
{
result = 0;
}
else
{
for(k=2;k<=j/2;k++)
{
if(j%k == 0)
{
result = 1;
break;
}
}
}
if(result == 0)
return 1;
else
return 0;
}
main (void)
{
printf("Please enter the value of n: ");
scanf("%d", &n);
printf("Please enter the value of m: ");
scanf("%d", &m);
if (n < 4)
{
num = 4;
}
else
{
if (n%2 != 0)
{
num = n + 1;
}
}
for (i = num; i <= m; i += 2)
{
if(goldbach(i, a, b))
{
printf("%d = %d + %d \n", i, a, b);
}
else
{
printf("Goldbach's Conjecture fails for this number: %d \n", i);
}
}
}
Any ideas?
I'm probably just overlooking something obvious, but damned if my eyes aren't beginning to rebel.
Thanks in advance for taking the time to look it over