Thread: Godbach conjecture!!

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    103

    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);
                              }
                              }
                     }
                     }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    it's goldbach and not Godbach, sorry everyone!

  3. #3
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    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
    straight off the heap

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    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??

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Leojeen View Post
    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.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ya I see..but is there any other possible solution to the problem, without using an array?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do the for loop correctly? (I was not suggesting that your for-loop on a was right.)

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    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??

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    I guess there is not!!

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Leojeen View Post
    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.
    Last edited by tabstop; 04-20-2008 at 05:34 PM.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ya that's right , it shows me all the possible cases this time, but i can handle it..
    thanks tabstop ..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Goldbach's Conjecture
    By cashmerelc in forum C Programming
    Replies: 7
    Last Post: 07-19-2010, 10:41 PM
  2. Goldbach's conjecture
    By dcwang3 in forum C Programming
    Replies: 10
    Last Post: 10-14-2008, 01:34 AM
  3. Goldbach Conjecture
    By StarOrbs in forum C++ Programming
    Replies: 19
    Last Post: 03-17-2005, 04:42 PM