# Godbach conjecture!!

• 04-20-2008
Leojeen
Godbach conjecture!!
Hi everyone,
Its my first post, and by the way its my first semester studying C language. And it's the first programming experience.
I was asked to write a program that , for each even number between 700 and 1100 , it lists its equivalent value as a sum of primes (assuming godbach was right so far!! lol ) ..the thing is that i couldn't do it using ordinary loops. so i used an array in which i have stored the primes i want to process.
For that said, Can anyone tell me why the output of my program outputs only relatively bigger primes as operands for the sum. I mean it's correct but why it skip situations in which small prime numbers can be used like 3.
700 = 419 + 281 ya right but why not 700 = 23 + 677 .
here is my code

Code:

```#include <stdio.h> #include <stdlib.h> #include <math.h> #define start 700 #define finish 1100 int is_prime(int); int check(int); int main(){           for(int i  = start ; i <= finish ; i += 2){                   check(i);                   }           system("PAUSE");           return(0);     } int is_prime(int n){                 for(int i = 2 ; i <= sqrt(n) ; i++){                           if(!(n%i))                                   return(0);                 }                 return(1);     } int check(int x){                 int k[x];                 int j = 0;                 for (int i = 1 ; i < x ; i += 2){                           if (is_prime(i)){                             k[j] = i;                             j++;                           }                 }                                        for (int n = 0 ; n < j ; n++){                           for (int a = 0 ; a <= j/2 ;a++){                           if (x == k[n]+k[a]){                           printf("%d = %d + %d\n",x,k[n],k[a]);                           return (0);                           }                           }                 }                 }```
• 04-20-2008
Leojeen
it's goldbach and not Godbach, sorry everyone!
• 04-20-2008
dinjas
Code:

``` for (n = 0 ; n < j ; n++) {   for (a = 0 ; a <= j/2 ;a++) {       if (x == k[n]+k[a]) {         printf("%d = %d + %d\n",x,k[n],k[a]);         return (0);       }   } }```
You are having problems because you return before you have a chance to find those other cases
• 04-20-2008
Leojeen
Ya but if remove that return, than the program will list all possible cases. which was not asked..
anyway can anybody tell me how to use just loops to solve the problem??
• 04-20-2008
tabstop
Quote:

Originally Posted by Leojeen
Ya but if remove that return, than the program will list all possible cases. which was not asked..
anyway can anybody tell me how to use just loops to solve the problem??

If you mean, why is it picking middle numbers, then the answer is
Code:

`for (int a = 0; a <= j/2; a++)`
which says that when the first prime is 23, the second prime can only go up to 11.
• 04-20-2008
Leojeen
Ya I see..but is there any other possible solution to the problem, without using an array?
• 04-20-2008
tabstop
Do the for loop correctly? (I was not suggesting that your for-loop on a was right.)
• 04-20-2008
Leojeen
But my program works!! I'm talking about a way without using an array in the function check, it could be some nested loops..right??
• 04-20-2008
Leojeen
I guess there is not!!
• 04-20-2008
tabstop
Quote:

Originally Posted by Leojeen
But my program works!! I'm talking about a way without using an array in the function check, it could be some nested loops..right??

If the program works the way you want it to, why are you asking the question? If you want it to give you 23+677 instead of what you're getting, why haven't you fixed the program as indicated? If something else is the matter, why aren't you telling us what it is?

If you don't want to store results in an array, do the isprime() check inside the for loops instead of beforehand.
• 04-20-2008
Leojeen
Ya that's right , it shows me all the possible cases this time, but i can handle it..
thanks tabstop ..